Change default URL param name when using Route::resource - laravel

When using Route::resource(), Laravel of course 'chooses' somethings for you, such as route names and methods.
I know how to override, for example, the allowed routes/methods:
Route::resource('user', 'UserController', array('only' => array('index', 'show')));
But I now need to override the URL param name that Laravel sets for the user routes. By default, in the example above, it will be user. But, I want it to be user_id.
Does Laravel provide a way to set this, when using Route::resource?
So that, for example, I would end up with the route:
mydomain.com/users/{user_id}
rather than:
mydomain.com/users/{user}
Thanks

Found it. Yes, Laravel does provide a way to override this when using Route::resource().
Route::resource('users', 'UserController')->parameters([
'users' => 'user_id'
]);
The key of the element in the array in the argument to 'parameters' is the same as what you enter as the first argument of the 'resource' method (not the 'singular' version or something)

Related

Multiple scoping resource route

i was create route resource and i use {post:slug} for route model binding but in some method for example update method, i want to use {post:id} for route model binding and keep the other method to use {post:slug}
How to implement it? Is it possible to add multiple scope
this is my code
Route::resource('/dashboard/posts', DashboardPostController::class)->scoped(['post' => 'slug']);
First, you need to use resources without update:
Route::resource('/dashboard/posts', DashboardPostController::class)->scoped(['post', 'slug'])->except('update');
and then:
Route::put('dashboard/posts/{posts:id}, [ DashboardPostController::class => 'update');

How to pass a variable as a route parameter when accessing the route in Laravel?

This does not work:
return redirect('/view-project-team/' . $projectRequest->id );
What is the right way to pass a variable into the route in this context?
As was said in comments you should use name of the route:
return redirect()->route('view.project.team', ['id' => $projectRequest->id]);
Names can be defined in your router:
Route::get('/view-project-team/{id}', 'YourController#yourHandler')->name('view.project.team');
Note that:
Dots in name of the route are not necessary (you could give any name).
'id' in route() call is refer to {id} in Route::get() call (names must match).

Route User Role Laravel 5.4

I'm very confused on this situation. I have two routes with on resource name.
Route::resource('product', 'Product\AreaManagerProductController');
Route::resource('product', 'Product\SystemAdminProductController');
I need to make it as one because I have a contextual binding.
$this->app->when(AreaManagerProductController::class)
->needs(ProductInterface::class)
->give(AreaManagerProductRepository::class);
$this->app->when(SystemAdminProductController::class)
->needs(ProductInterface::class)
->give(SystemAdminProductRepository::class);
The contextual binding works fine... but I need to change my route like this.
Route::resource('product_area_manager', 'Product\AreaManagerProductController');
Route::resource('product_system_admin', 'Product\SystemAdminProductController');
I created ProductController and some kind of weird solution.
public function index(){
//Create a conditional statement base on user
return app('App\Http\Controllers\Product\AreaManagerProductController')->index();
}
It may work but it doesn't trigger the middleware... What could be the best practice on this situation. TY
You can have your Route like this
Route::group(['prefix' => 'product', 'namespace' => 'Product', 'middleware' => '<your middleware>'], function() {
Route::resource('area_manager', 'AreaManagerController');
Route::resource('system_admin', 'SystemAdminController');
});
The reason I grouped the route is to reduce redundancy, and the reason i removed Product from the controller name is, as there is a namespace Product already, there is no need of long Class names.
If you wan to access some methods in the AreaManagerController and SystemAdminController just extend the ProductController to these Controllers.
If you want to add some specific middleware for the actions inside these controllers, I have added a middleware clause in the route group which will affect to these controllers, if not needed just remove it.
Hope this helps you.

Change name of route:resource parameter

I have this route:
Route::group(['middleware' => 'admin'], function () {
Route::resource('backoffice/home', 'Backoffice\Home');
Route::resource('backoffice/ProductBasic', 'Backoffice\ProductBasic');
});
Laravel by default uses "ProductBasic" as parameter. As can be requested with
Request::route()->parameters();
The return on dd() will be
array:1 [▼
"ProductBasic" => "1"
]
I would like to adjust of every var. How can i do that while still using Route::resource as a method of creating my routes.
Edit:
Goals is to use the parameter outside a controller, in a composer. By using 1 parameter for a group of routes (and another for another group...) i can create a dynamic menu.
If(isset(parameter)){
use parameter value to do something
}
I know i can pass parameters to the view and access the values in the view, but i think it's a cleaner solution this way.

How do I set dynamic prefixes in Laravel routes

heres the situation - I'm building a site thats region based and I need to set it up so that the first segment of the route is a region i.e. of the type:
www.mysite.com/{region}
Currently I have routes set up like this:
www.mysite.com/businesses
www.mysite.com/businesses/show
I've tried a number of tricks but for some reason I can't get this to work:
www.mysite.com/{region}/businesses
in such a way that I need to filter by the {region} variable and that the {region} variable has to prepend every url in the site, also the {region} variable should also be accessible in the controller. I had a look at the localization options except I don't want to change languages here. I'm looking for implementing something of the following:
www.mysite.com/barcelona/businesses
www.mysite.com/new-york/businesses
I already have a table of all regions and slugs and the required relationships. Just trying to get this to work.
Add the region route on top of all other routes, I have similar feature for a project and that fixed it for me.
Route::get('{region?}/businesses', array(
'as' => 'regionBusinesses',
'uses' => 'RegionBusinessesController#getBusinesses'
))->where('region', '.*');
In your Controller
class RegionBusinessesController extends SomeController {
public function getBusinesses($region) {
return View::make('YOUR_VIEW_NAME')->withBusinesses($this->YOUR_MODEL->FETCH_BUSINESSES_METHOD($region));
}

Resources