How to Create Resource Route and Resource Controller?

Admin   Laravel   180  2021-03-16 07:00:07

Hi Dev,

In this blog, I will teach you how create resource route and resource controller in laravel app. In every application. It's common to create, read, edit, delete functionally.

You can easy to understand a topic of resource route and resource controller in laravel. You need to just follow this tutorial.

Laravel resource route and resource controller is proveide many features first you have to create resource route on laravel they provide insert, update, view and delete route. and resource controller provide method for insert, update, view and delete.

So, In this example we will see how to create resource route.

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

CRUD Route

 

Route::get('blogs',['as'=>'blogs.index','uses'=>'[email protected]']);

Route::post('blogs/create',['as'=>'blogs.store','uses'=>'[email protected]']);

Route::get('blogs/edit/{id}',['as'=>'blogs.edit','uses'=>'[email protected]']);

Route::patch('blogs/{id}',['as'=>'blogs.update','uses'=>'[email protected]']);

Route::delete('blogs/{id}',['as'=>'blogs.destroy','uses'=>'[email protected]']);

Route::get('blogs/{id}',['as'=>'blogs.view','uses'=>'[email protected]']);

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

Resource Route

 

Route::resource('blogs', 'BlogController');

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 | blogs | blogs.index | App\Http\Controllers\[email protected] | web |

| | POST | blogs | blogs.store | App\Http\Controllers\[email protected] | web |

| | GET|HEAD | blogs/create | blogs.create | App\Http\Controllers\[email protected] | web |

| | GET|HEAD | blogs/{blog} | blogs.show | App\Http\Controllers\[email protected] | web |

| | PUT|PATCH | blogs/{blog} | blogs.update | App\Http\Controllers\[email protected] | web |

| | DELETE | blogs/{blog} | blogs.destroy | App\Http\Controllers\[email protected] | web |

| | GET|HEAD | blogs/{blog}/edit | blogs.edit | App\Http\Controllers\[email protected] | web |

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

Ok, Now create resource controller using bellow command. So, Let's run bellow command.

Resource Controller Command

 

php artisan make:controller BlogController --resource --model=Blog

After successfully run this command. You can see your BlogController with resource method, So lets open and see.

app/Http/Controllers/BlogController.php

 

<?php

namespace App\Http\Controllers;

use App\Blog;

use Illuminate\Http\Request;

class BlogController 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\Blog $blog

* @return \Illuminate\Http\Response

*/

public function show(Blog $blog)

{

}

/**

* Show the form for editing the specified resource.

*

* @param \App\Blog $blog

* @return \Illuminate\Http\Response

*/

public function edit(Blog $blog)

{

}

/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param \App\Blog $blog

* @return \Illuminate\Http\Response

*/

public function update(Request $request, Blog $blog)

{

}

/**

* Remove the specified resource from storage.

*

* @param \App\Blog $blog

* @return \Illuminate\Http\Response

*/

public function destroy(Blog $blog)

{

}

}

It will help you....