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 class="modal fade" id="editModal{{ $product->ID_produk }}" tabindex="-1"
aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form action="/admin/data-produk/{{ $product->ID_produk }}" method="POST" class="d-inline">
@method('PUT')
@csrf
<div class="modal-header">
<h5 class="modal-title" id="eeditModalLabel">Edit data produk</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="ID_produk" class="col-form-label">ID: </label>
<input type="text" class="form-control" name="ID_produk" id="ID_produk"
value="{{ $product->ID_produk }}">
</div>
<div class="mb-3">
<label for="nama_produk" class="col-form-label">Nama: </label>
<input type="text" class="form-control" name="nama_produk" id="nama_produk"
value="{{ $product->nama_produk }}">
</div>
{{-- <div class="mb-3">
<label for="gambar_produk" class="col-form-label">Gambar: </label>
<input type="file" class="custom-file-input" name="gambar_produk" id="gambar_produk">
</div> --}}
<div class="mb-3">
<label for="deskripsi_produk" class="col-form-label">Deskripsi:</label>
<textarea class="form-control" name="deskripsi_produk" id="deskripsi_produk" rows="10">
{{ $product->deskripsi_produk }}</textarea>
</div>
<div class="mb-3">
<label for="harga_produk" class="col-form-label">Harga: </label>
<input type="text" class="form-control" name="harga_produk" id="harga_produk"
value="{{ $product->harga_produk }}">
</div>
<div class="mb-3">
<label for="slug" class="col-form-label">Slug: </label>
<input type="text" class="form-control" name="slug" id="slug"
value="{{ $product->slug }}">
</div>
<div class="mb-3">
<label for="stock" class="col-form-label">Stock: </label>
<input type="text" class="form-control" name="stock" id="stock"
value="{{ $product->stock }}">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">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:
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