Home > Software design >  Laravel Resource Not Updating Table
Laravel Resource Not Updating Table

Time:07-09

I have a blade project for CRUD with resource type controller in Laravel. it did using update method, but not updating anything to table, but it sending a value in dd. how do i fix it ?

here the code

1. Blade

@foreach ($produk as $product)
    <div  id="editModal{{ $product->ID_produk }}" tabindex="-1"
        aria-labelledby="editModalLabel" aria-hidden="true">
        <div >
            <div >
                <form action="/admin/data-produk/{{ $product->ID_produk }}" method="POST" >
                    @method('PUT')
                    @csrf
                    <div >
                        <h5  id="eeditModalLabel">Edit data produk</h5>
                        <button type="button"  data-bs-dismiss="modal"
                            aria-label="Close"></button>
                    </div>
                    <div >
                        <div >
                            <label for="ID_produk" >ID: </label>
                            <input type="text"  name="ID_produk" id="ID_produk"
                                value="{{ $product->ID_produk }}">
                        </div>
                        <div >
                            <label for="nama_produk" >Nama: </label>
                            <input type="text"  name="nama_produk" id="nama_produk"
                                value="{{ $product->nama_produk }}">
                        </div>
                        {{-- <div >
                        <label for="gambar_produk" >Gambar: </label>
                        <input type="file"  name="gambar_produk" id="gambar_produk">
                    </div> --}}
                        <div >
                            <label for="deskripsi_produk" >Deskripsi:</label>
                            <textarea  name="deskripsi_produk" id="deskripsi_produk" rows="10">
                                {{ $product->deskripsi_produk }}</textarea>
                        </div>
                        <div >
                            <label for="harga_produk" >Harga: </label>
                            <input type="text"  name="harga_produk" id="harga_produk"
                                value="{{ $product->harga_produk }}">
                        </div>
                        <div >
                            <label for="slug" >Slug: </label>
                            <input type="text"  name="slug" id="slug"
                                value="{{ $product->slug }}">
                        </div>
                        <div >
                            <label for="stock" >Stock: </label>
                            <input type="text"  name="stock" id="stock"
                                value="{{ $product->stock }}">
                        </div>
                    </div>
                    <div >
                        <button type="button"  data-bs-dismiss="modal">Close</button>
                        <button type="submit" >Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endforeach

2. Route

Route::resource('/admin/data-produk', AdminProdukController::class)->middleware('auth');

3. Controller

public function update(Request $request, Produk $product)
    {
        $validatedData = $request->validate([
            'nama_produk' => ['required'],
            'deskripsi_produk' => ['required'],
            'harga_produk' => ['required'],
            'slug' => ['required'],
            'stock' => ['required']
        ]);
        Produk::where('ID_produk', $product->ID_produk)
                ->update($validatedData);   
                
        return back()->with('berhasilEdit', 'Data produk berhasil diedit!');
    }

4. Model

class Produk extends Model{
    use HasFactory;
    
    protected $table = 'tabel_produk';
    protected $primaryKey = 'ID_produk';
    public $timestamps = false;
    protected $fillable = ['nama_produk',
    'deskripsi_produk',
    'harga_produk',
    'slug',
    'stock'
    ];
}

VALUE I do dd($validatedData); in controller after editing the value, and it shown here: enter image description here

CodePudding user response:

in your controller do this

    public function update(Request $request,$productID)
        {
            $validatedData = $request->validate([
                'nama_produk' => ['required'],
                'deskripsi_produk' => ['required'],
                'harga_produk' => ['required'],
                'slug' => ['required'],
                'stock' => ['required']
            ]);
        $product=Produk::find($productID);
        $product=$product->update($validateData);
        return back()->with('berhasilEdit', 'Data produk berhasil diedit!');
        }

after that if you do dd($product) you will get updated product Value

  • Related