How To Create Nested Resources Routes With Laravel

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

Laravel resource controllers provide the CRUD routes to the controller in a single line of code. A resource controller is used to create a controller that handles all the http requests stored by your application. The resource() is a static function like get() method that gives access to multiple routes that we can use in a controller.

How To Create Nested Resources Routes With Laravel

Nested Resources

To know more about creating resource controller visit our Bajarangisoft site.

Sometimes you may need to define routes to a nested resource. For example, a photo resource may have multiple comments that may be attached to the photo. To nest the resource controllers, use "dot" notation in your route declaration:

 

Route::resource('photos.comments', 'PhotoCommentController');


This route will register a nested resource that may be accessed with URIs like the following:

/photos/{photo}/comments/{comment}


Shallow Nesting

Often, it is not entirely necessary to have both the parent and the child IDs within a URI since the child ID is already a unique identifier. When using unique identifier such as auto-incrementing primary keys to identify your models in URI segments, you may choose to use "shallow nesting":
 

Route::resource('photos.comments', 'CommentController')->shallow();


The route definition above will define the following routes:
 

Verb URI Action Route Name
GET /photos/{photo}/comments index photos.comments.index
GET /photos/{photo}/comments/create create photos.comments.create
POST /photos/{photo}/comments store photos.comments.store
GET /comments/{comment} show comments.show
GET /comments/{comment}/edit edit comments.edit
PUT/PATCH /comments/{comment} update comments.update
DELETE /comments/{comment} destroy comments.destroy


1.Create a resource controller, run:

php artisan make:controller PostController --resource
 

2.Create a controller with stubbed out methods for handling typical CRUD actions.

The Laravel resourceful route goes hand-in-hand with the resource controller. To generate typical CRUD routes to the controller, add this line to routes/web.php

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

3.Route declaration sets up multiple routes to the controller. You can view these routes by running php artisan route:list:
NESTED ROUTES

 

Nested routes allow you to capture a relationship between resources within your routing. For example, resources from a Post model might be nested under a User model: users/{user}/posts/{post}. The resulting URL might look like this: http://laraveldemoproject/users/1/posts/10.

To create nested routes, Laravel lets you use a dot notation. Sticking with the previous example:

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

This would create a set of routes for posts that include the user identifier. For example:

  • The ‘index’ route: http://localhost/laraveldemoproject/public/users/1/posts
  • The ‘show’ route: http://laraveldemoproject/public/users/1/posts/10

You can override the out-of-the-box routes that are set up with the resource() method, or add additional routes - you should add overrides before the resource method.

Nesting resources can make the URLs for your project unwieldy. They may also add complexity to your controllers - for example, the generated controller method stubs may require additional parameters, and you may need to pass additonal parameters when building forms.

postController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class postController extends Controller
{
    public function index() {
        return 'Post index';
    }
    public function create() {
        return 'Post create';
    }
    public function store(Request $request) {
        return 'Post store';
    }
    public function show($id) {
        return 'Post show';
    }
    public function edit($id) {
        return 'Post edit';
    }
    public function update(Request $request, $id) {
        return 'Post update';
    }
    public function destroy($id) {
        return 'Post destroy';
    }
}
 

UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller {

    public function index() {
        return 'User index';
    }
    public function create() {
        return 'User create';
    }
    public function store(Request $request) {
        return 'User store';
    }
    public function show($id) {
        return 'User show';
    }
    public function edit($id) {
        return 'User edit';
    }
    public function update(Request $request, $id) {
        return 'User update';
    }
    public function destroy($id) {
        return 'User destroy';
    }
}


 

Related Post