PASSWORD RESET

Your destination for complete Tech news

What is resource routes in Laravel and how to use it?

694 0
2 min read

In Laravel, resource routes are routes that are defined for a resourceful controller. A resourceful controller is a controller that is used to handle all the actions related to a resource, such as creating, reading, updating, and deleting a resource.

Resource routes are useful because they provide a way to define all the routes needed for a resource in a single place, using a simple and expressive syntax. They can also handle routing for a number of different actions and can be easily extended or customized.

To use resource routes in Laravel, you can define them in your routes file (e.g., routes/web.php) using the Route::resource method. The Route::resource method takes the name of the resource as the first argument and the name of the controller handling the resource as the second argument.

Here’s an example of how you can define a resource route for a resource called posts:

Route::resource('posts', 'PostController');

This will define the following routes for the posts resource:

+--------+----------+-----------------+------+-----------------------------------------------+------------+
| Method | URI      | Name            | Action                                            | Middleware |
+--------+----------+-----------------+------+-----------------------------------------------+------------+
| GET    | /posts   | posts.index     | App\Http\Controllers\PostController@index         | web        |
| GET    | /posts/create | posts.create | App\Http\Controllers\PostController@create       | web        |
| POST   | /posts   | posts.store     | App\Http\Controllers\PostController@store         | web        |
| GET    | /posts/{post} | posts.show   | App\Http\Controllers\PostController@show         | web        |
| GET    | /posts/{post}/edit | posts.edit | App\Http\Controllers\PostController@edit       | web        |
| PUT    | /posts/{post} | posts.update | App\Http\Controllers\PostController@update       | web        |
| DELETE | /posts/{post} | posts.destroy | App\Http\Controllers\PostController@destroy     | web        |
+--------+----------+-----------------+------+-----------------------------------------------+------------+

You can then create a controller for the resource and define the actions for each route. For example, the index action might retrieve a list of all the posts and return them to the view, while the store action might save a new post to the database.

You can also customize the resource routes by specifying only the actions you want to include using the only or except methods. For example:

Route::resource('posts', 'PostController')->only(['index', 'show']);

This will only include the index and show actions in the resource routes.

Route::resource('posts', 'PostController')->except(['create', 'store', 'update', 'destroy']);

This will include all the resource routes except for the create, store, update, and destroy actions.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.