How to create Laravel Resource Controller example?

By Hardik Savani | April 12, 2017 | | 7010 Viewer | Category : Laravel


Share this post:

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 | App\Http\Controllers\ItemController@index | web |

| | POST | items | items.store | App\Http\Controllers\ItemController@store | web |

| | GET|HEAD | items/create | items.create | App\Http\Controllers\ItemController@create | web |

| | GET|HEAD | items/{item} | items.show | App\Http\Controllers\ItemController@show | web |

| | PUT|PATCH | items/{item} | items.update | App\Http\Controllers\ItemController@update | web |

| | DELETE | items/{item} | items.destroy | App\Http\Controllers\ItemController@destroy | web |

| | GET|HEAD | items/{item}/edit | items.edit | App\Http\Controllers\ItemController@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

<?php


namespace App\Http\Controllers;


use App\Item;

use Illuminate\Http\Request;


class ItemController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{


}


/**

* Show the form for creating a new resource.

*

* @return \Illuminate\Http\Response

*/

public function create()

{


}


/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{


}


/**

* Display the specified resource.

*

* @param \App\Item $item

* @return \Illuminate\Http\Response

*/

public function show(Item $item)

{


}


/**

* Show the form for editing the specified resource.

*

* @param \App\Item $item

* @return \Illuminate\Http\Response

*/

public function edit(Item $item)

{


}


/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param \App\Item $item

* @return \Illuminate\Http\Response

*/

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

{


}


/**

* Remove the specified resource from storage.

*

* @param \App\Item $item

* @return \Illuminate\Http\Response

*/

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...



We are Recommending you: