How to create Laravel Resource Controller example?

How to create Laravel Resource Controller example?

In this post we will give you information about How to create Laravel Resource Controller example?. Hear we will give you detail about How to create Laravel Resource Controller example?And how to use it also give you demo for it if it is necessary.

In this post we are going to learn how to use resource controller using resource route in Laravel 5 application from scratch.

Laravel resource controller is pretty interesting feature to create quick CRUD application in laravel. For resource you have to do two things on laravel application. first you have to create resource route on laravel they provide insert, update, view, delete routes and second you have to create resource controller that will provide method for insert, update, view and delete.

So, in this example we will see how to create resource route and how they work.

First we have to understand why we choose resource route instead of different route. we always declare different different route for our crud application like as bellow:

CRUD Route:

Route::get('items',['as'=>'items.index','uses'=>'ItemController@index']);

Route::post('items/create',['as'=>'items.store','uses'=>'ItemController@store']);

Route::get('items/edit/{id}',['as'=>'items.edit','uses'=>'ItemController@edit']);

Route::patch('items/{id}',['as'=>'items.update','uses'=>'ItemController@update']);

Route::delete('items/{id}',['as'=>'items.destroy','uses'=>'ItemController@destroy']);

Route::get('items/{id}',['as'=>'items.view','uses'=>'ItemController@view']);

As you can see above route declare, we have to create six routes for our crud application module. But we can simply create those six routes by using bellow resource route:

Resource Route:

Route::resource('items', 'ItemController');

Now, you can run bellow command and check create route lists:

php artisan route:list

Now we have output as like bellow created route:

+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

| Domain | Method | URI | Name | Action | Middleware |

+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

| | GET|HEAD | api/user | | Closure | api,auth:api |

| | GET|HEAD | items | items.index | AppHttpControllersItemController@index | web |

| | POST | items | items.store | AppHttpControllersItemController@store | web |

| | GET|HEAD | items/create | items.create | AppHttpControllersItemController@create | web |

| | GET|HEAD | items/{item} | items.show | AppHttpControllersItemController@show | web |

| | PUT|PATCH | items/{item} | items.update | AppHttpControllersItemController@update | web |

| | DELETE | items/{item} | items.destroy | AppHttpControllersItemController@destroy | web |

| | GET|HEAD | items/{item}/edit | items.edit | AppHttpControllersItemController@edit | web |

+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

Ok, now we have to create resource controller by using bellow command, so let’s run bellow command and check ItemController in your app directory:

Resource Controller Command:

php artisan make:controller ItemController --resource --model=Item

After successfully run above command, you can see your ItemController with following resource method, So let’s open and see.

app/Http/Controllers/ItemController.php

Also see:Laravel 5 Ajax CRUD with Pagination example and demo from scratch

<?php


namespace AppHttpControllers;


use AppItem;

use IlluminateHttpRequest;


class ItemController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index()

{


}


/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function create()

{


}


/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{


}


/**

* Display the specified resource.

*

* @param AppItem $item

* @return IlluminateHttpResponse

*/

public function show(Item $item)

{


}


/**

* Show the form for editing the specified resource.

*

* @param AppItem $item

* @return IlluminateHttpResponse

*/

public function edit(Item $item)

{


}


/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param AppItem $item

* @return IlluminateHttpResponse

*/

public function update(Request $request, Item $item)

{


}


/**

* Remove the specified resource from storage.

*

* @param AppItem $item

* @return IlluminateHttpResponse

*/

public function destroy(Item $item)

{


}

}

Ok, this way you can simply use resource route and controller, make quick crud module for your project.

I hope It can help you…

Hope this code and post will helped you for implement How to create Laravel Resource Controller example?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

88  +    =  94

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US