Home > database >  Laravel 8 Route in blade
Laravel 8 Route in blade

Time:12-11

I have the following web routes in my Laravel project

Route::resource('client/{client}/users', UserController::class);
Route::resource('users', UserController::class);

For the latter I am able to use {{ route('users.create') }} in my blade file, but I'm not sure what to put for the other one.

I need to 2 ways separate resources because some users are able to edit users from their own client (/users) and users of other clients (/client/123/users)

`php artisan route:list shows that I'm getting the same name for both. What's the correct way to name the clients-users resource?

|        | GET|HEAD  | client/{client}/users             | users.index                     | App\Http\Controllers\UserController@index                                       | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | POST      | client/{client}/users             | users.store                     | App\Http\Controllers\UserController@store                                       | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | client/{client}/users/create      | users.create                    | App\Http\Controllers\UserController@create                                      | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | PUT|PATCH | client/{client}/users/{user}      | users.update                    | App\Http\Controllers\UserController@update                                      | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | DELETE    | client/{client}/users/{user}      | users.destroy                   | App\Http\Controllers\UserController@destroy                                     | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | client/{client}/users/{user}      | users.show                      | App\Http\Controllers\UserController@show                                        | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | client/{client}/users/{user}/edit | users.edit                      | App\Http\Controllers\UserController@edit                                        | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |

and

|        | GET|HEAD  | users                             | users.index                     | App\Http\Controllers\UserController@index                                       | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | users/create                      | users.create                    | App\Http\Controllers\UserController@create                                      | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | DELETE    | users/{user}                      | users.destroy                   | App\Http\Controllers\UserController@destroy                                     | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | PUT|PATCH | users/{user}                      | users.update                    | App\Http\Controllers\UserController@update                                      | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | users/{user}                      | users.show                      | App\Http\Controllers\UserController@show                                        | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | users/{user}/edit                 | users.edit                      | App\Http\Controllers\UserController@edit                                        | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |

CodePudding user response:

The third arguement of the resource is an array of options, one of which allows you to specifiy a prefix.

Route::resource('client/{client}/users', UserController::class, ['as' => 'clients']);
Route::resource('users', UserController::class);

Now your routes should be

clients.users.{action}
users.{action}

CodePudding user response:

You need to give it a name. That said, I don't think you want Route::resource for that route.

I'm gonna give it the name clients.users.index but you can give it whatever name you want.

Route::get('clients/{client}/users', [ClientController::class, 'users'])->name('clients.users.index');
{{ route('clients.users.index') }}

The way to handle nested resources would be to declare the route:

Route::resource('clients.users', SomeOtherController::class);
  • Related