PART 7 - ADDING EDIT AND DELETE PRODUCT OPTION

Опубликовано: 28 Март 2023
на канале: Leo Paliuanan
122
1

Topic: LARAVEL PROJECT (E-SHOP) CAPSTONE PROJECT

PART 7 - ADDING EDIT AND DELETE PRODUCT OPTION


lets start
1 - lets add a link for edit and delete
open the blade for the viewing of products
add a link
add the ff:

a href="" class="btn btn-sm btn-primary" Edit /a
a href="" class="btn btn-sm btn-danger" Delete /a



2 - run php artisan route:list and look for edit
add {{route('seller.products.edit',$prod- id)}} on the href of the edit link


looks like this

a href="{{route('seller.products.edit',$prod- id)}}" class="btn btn-sm btn-primary" Edit /a



3- create a blade for the form to edit where we can edit the product

go to resource - view - then add edit.blade.php
in my case - I copied create.blade.php and renamed it as edit.blade.php

change code accordingly






4 - go to the controller and look for function edit and add the ff:


$p = products::find($id);

return view('seller.products.edit')
with('p',$p);




5- add value to each of the input using : value="{{$p- fieldname}}"
looks like this
Note: make sure to add value for each of the input you have for the forms







6 change the route, add id on the form action and change the method to PUT

looks like this:

form action="{{route('seller.products.update',$p- id)}}" method="post" class="was-validated"
@csrf
@method('PUT')






7- go back to the controller and look for the update function

add the ff code:

$prd = products::where('id','=',$id)
update([
'prodcategory' = strtoupper($request- input('cat')),
'prodname' = strtoupper($request- input('name')),
'proddesc' = strtoupper($request- input('desc')),
'price' = $request- input('price'),
'stock' = $request- input('stock'),
]);

return redirect('/seller/products');

================= done with the simple edit ============