1. Make resource controller with the following command
php artisan make:controller CustomerController -r
2. There are now following routes generated
3. Then in web.php file make a base route for CustomerController with
Route::resource('/customers', CustomerController::class);
route('customers.index')
route('customers.store')
route('customers.edit')
route('customers.update')
route('customers.destroy')
4. Now First of all show data in index by click the route button
<a href="{{route('customers.index')}}">Cutomer List</a>
5. Now create a button to create a new customer
<a href="{{{{route('customers.create')}}}}"></a>
6. now create page of new cutomer form
<form method="post" action= "{{route('customers.create')}}" enctype="multipart/form-data">
@csrf
<label for="name">Name</label>
<input type="text" name="name">
<input type="submit">
</form>
7. Now in controller enter the following code in store method in CustomerController
$customer = new Customer;
$customer->name = $customer->name;
$result = $customer->save();
if($result)
{
return redirect()->route('customers.index')->with('success', 'Record Saved Successfully');
}
else
{
return redirect()->route('customers.index')->with('success', 'Unable to Save');
}
8. Now create a button to edit the data
<a href="{{route('customers.edit')}}">Edit Customer</a>
9. Now in the edit blade file enter the following code
<form method="post" action= "{{route('customers.update', $customer->id)}}" enctype="multipart/form-data">
@csrf
@method('put')
<label for="name">Name</label>
<input type="text" name="name" value="{{$customer->name}}">
<input type="submit">
</form>
10. Now enter the following code in update method of CustomerController
$customer = Customer::find($id);
$customer->name = $customer->name;
$result = $customer->update();
if($result)
{
return redirect()->route('customers.index')->with('success', 'Record Saved Successfully');
}
else
{
return redirect()->route('customers.index')->with('success', 'Unable to Save');
}
10. Now create a delete button on the index view
<form method="post" action="{{route('customers.destroy', $customer->id)}}">
<button onclick="return confirm('Are you sure to Delete?')" type="submit">Delete</button>
</form>
11. Now in the destroy method enter the following code
$customer = Customer::find($id);
$result = $customer->delete();
if($result)
{
return redirect()->route('customers.index')->with('success', 'Record deleted Successfully');
}
else
{
return redirect()->route('customers.index')->with('success', 'Unable to Delete Record');
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter