Pada view form action diisi dengan route dan parameter yang berisi id item yang ingin di ubah/update.
Khusus form update pada laravel harus ditambahkan @method(‘PUT’) didalam formnya
//View
<form action="{{route('products.update', $item->id)}}" method="POST">
@method('PUT')
@csrf
<div class="form-group">
<label for="name" class="form-control-label">Nama Barang</label>
<input type="text" name="name" value="{{old('name') ? old('name') : $item->name}}" class="form-control @error('name') is-invalid @enderror">
@error('name') <div class="text-muted">{{$message}}</div> @enderror
</div>
<div class="d-grid gap-2">
<button class="btn btn-primary btn-block" type="submit">Tambah Barang</button>
</div>
</div>
</form>
//Controller
public function update(ProductRequest $request, $id)
{
$data = $request->all();
$data['slug'] = Str::slug($request->name);
$item = Product::findOrFail($id);
$item->update($data);
return redirect()->route('products.index');
}
//Model
class Product extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable = [
'name', 'type', 'description', 'price', 'slug', 'quantity'
];
protected $hidden = [
];
public function galleries()
{
return $this->hasMany(ProductGallery::class, 'products_id');
}
}
Leave a Reply