John Davidson

php - RouteName and ModelName is same in Controller resource problem

0 comments
Message:




is there a way for the routeName (number 1) and modelName(number 2) to be different ??
(define both separately)



My Answer:
Yes, you can define the route name and model name separately in a Laravel controller resource. Here's how you can achieve this:

1. Define the route name separately in your routes file (web.php or api.php):

php
Route::resource('posts', 'PostController')->names([
'index' => 'posts.index',
'create' => 'posts.create',
'store' => 'posts.store',
'show' => 'posts.show',
'edit' => 'posts.edit',
'update' => 'posts.update',
'destroy' => 'posts.destroy',
]);


2. Define the model name in your controller:

php
class PostController extends Controller
{
protected $modelName = 'Post';

// Controller methods
}


By defining the route name separately in the routes file and the model name in the controller, you can have different names for the route and model while still using the controller resource.

Rate this post

3 of 5 based on 8727 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us