Laravel 5.2 blade route to resource - laravel

The documentation says:
If the named route defines parameters, you may pass the parameters as
the second argument to the route function. The given parameters will
automatically be inserted into the URL in their correct positions:
Route::get('user/{id}/profile', ['as' => 'profile', function ($id) {
//
}]);
$url = route('profile', ['id' => 1]);
If i do have a nested resource route by using Route::resource(...) twice, i will get a named route wich contains a placeholder like
employees.{employee}.images.index
How can i create a route for this nested resource using the blade templating engine?
I thought about
route('employees.{employee}.images.index', ['employee' => $employee->id]);
but that does not work. I know i can "manually" create the routes, but this will make them less maintainable.
Update 1
I know i can name the routes manually and then use the given name. But if there is a way without naming them i would prefer it.

You don't have to add anything like employee into a route name. Run php artisan route:list command and you will see real names of all routes (look at column called Name), created by resource clause. Then just use them like usual:
route('employees.images.index', ['employee' => $employee->id]);
Also, you can name resource routes.

Related

Place a resource controller at root of website?

How can I use a resource controller at the root of my website? I have tried:
Route::resource('/', CategoryController::class);
But no luck.
Normally, you wouldn't do this as the first argument to that method is a resource name, not a path. Though, you can do this if you really want to by overriding the name of the route parameter that ends up getting used:
Route::resource('/', YourController::class)->parameters(['' => 'category']);
This would create the routes with a parameter named 'category':
GET {category} show
GET {category}/edit edit
PUT|PATCH {category} update
...
If you wanted to you could also update the names of these routes if needed via the names method.

Laravel: Resource Controller change parameter from ID to Slug

In my Laravel application I was using the normal routes, such as GET, POST, PUT and all with the various controllers. But as the application progresses, the routes/api.php file is turning out quite bulky. So, I am changing it with the "Resource Controller" syntax, for cleaner and reduced code.
However, earlier I was able to use the "token" or "slug" parameters in the url to fetch the data, for example: show/{token} & show($token) or show/{slug} & show($slug), but now whenever I try these or any other parameter I get 404 not found error. Therefore I am stuck with the default "id" parameter only.
Earlier:
Route::get('/categories', 'CategoryController#index');
Route::get('/categories/{token}', 'CategoryController#show');
Now:
Route::resource('/categories', 'CategoryController');
Is there any way to change the default "id" parameter to any other ..?
For Resource Routing the route parameter is the resource name, first argument, in singular form.
Route::resource('categories', 'CategoryController');
Will give you a route parameter of category. You can see this by running php artisan route:list and looking at the routes that are defined.
If you want to use a different parameter name for a resource you can also do that by overriding the parameter:
Route::resource('categories', 'CategoryController')->parameters([
'categories' => 'something',
]);
Now the route parameter used would be something.
This only has to do with the route parameter name, it has nothing to do with how any binding would be resolved, unless you had an Explicit Route Model Binding defined specifically for a parameter name.
If you are using Implicit Route Model Bindings you would define on your Model itself what field is used for the binding via the getRouteKeyName method. [In the next version of Laravel you will be able to define the field used in the route definition itself.]
Laravel 6.x Docs - Controllers - Resource Controllers - Naming Resource Parameters
Laravel 6.x Docs - Routing - Model Bindings - Implicit Route Model Binding getRouteKeyName
This can also be achieved by changing the customizing the key. In here slug refers to a column in the Category model.
Route::resource('categories', 'CategoryController')->parameters([
'categories' => 'categories:slug',
]);

How to write laravel 5.8 controllers routes similar to laravel 5.1

Having more than 20 controllers. It's very difficult to set each and every routes for add, edit and delete (also having more actions).
This is my laravel 5.1 routes.php :
Route::controllers([
'user' => 'UserController',
'taxes' => 'TaxController',
]);
Is there any way to support these routes in laravel 5.8?
You can use the Resource Controller and implement in routes/web.php. It will autogenerate the name for the route
//web.php
Route::resource('user', 'UserController');
Route::resource('taxes', 'TaxController');
Edit 1
If you want to exclude show method of the controller for the resource, You can add array inside the except method.
Route::resource('taxes', 'TaxController', [
'except' => ['show']
]);
Further, if you want to get only selected options, You can use only.
Route::resource('taxes', 'TaxController', [
'only' => ['index', 'create', 'store', 'edit']
]);
The controllers method was deprecated in Laravel 5.2. From the upgrade guide:
Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file.
1) Use Resource Routes
Provided that your controllers use the standard index, store, show etc methods you can simply use resource routes. For example:
Route::resource('user', 'UserController');
However if you want to exclude certain methods you can add them to the resource. For example:
Route::resource('user', 'UserController', ['except' => 'show']);
2) Declare Routes Explicitly
You can follow the Laravel 5.2 upgrade guide as above and instead declare each route explicitly.
3) Create a Macro
The Laravel router is Macroable. This means that you can add your own methods to it. For example, in your app service provider you could have the following:
Illuminate\Routing\Router::macro('controllers', function ($routes) {
// Create your own implementation of the controllers method.
});
This allows you to create your own implementation of the controllers method which means you wouldn't need to alter your routes or controllers, but you may need to dive in and look at Laravel's route handling to understand how to implement this.
I hope this helps.
You can use in the array, In as you call using routes. like {{route('claimsubmit')}}
Route::resource('claimform',array('as'=>'claimform','uses'=>'UserController#claimform');

Laravel 5.6 additional Route::resource() Parameters

I would like to know how to add additional parameters to Laravel's Route Resource without using Query Strings.
I created a controller (CustomerController) with all the built-in resources and then, added the following route:
Route::resource('customers', 'CustomerController');
What i would like to do is add additional parameters to some of the default resources without creating custom routes or using query strings. For example:
Default resource with optional parameter (index):
public function index($page = 0)
{
//...
}
Desired URL:
http://www.example.com/customers
http://www.example.com/customers/{page}
I tried the following, but i get a not found exception (NotFoundHttpException):
Route::resource('customers', 'CustomerController')->parameters([
'index' => 'page'
]);
Is this possible? If so, how can i accomplish it?
Resource Controllers must implement a defined set of methods which are then mapped to the appropriate HTTP verb and path by the router. These methods, paths and verbs form part of a contract that cannot be adjusted, otherwise working with a Laravel application that implements Resource Controllers would be a headache.
Resource Controllers excel in providing the same experience across all Laravel applications, if your application requires behaviour that isn't supported out of the box by Resource Controllers then it is a sign that you should not be using them and should instead register your routes manually.
If you have just one route that needs to implement custom behaviour then you can register some methods instead of all and then choose to register a custom route to your Resource Controllers method, something like:
Route::resource('customers', 'CustomerController')->except([
'index'
]);
Route::get('/customers/{page?}', 'CustomerController#index');
The documentation on Resource Controllers covers except and only.
Try this :
Route::resource('customers', 'CustomerController')->parameters([
'customers' => 'page'
]);
The example above generates the following URIs for the resource's show route:
/customers/{page}
you set the name route: 'index' replace it by the variable : 'customers' name of your ressource

how to pass variable from view to route in laravel

How to pass a variable from view to route in Laravel?
Here's the code at my route.php:
Route::get('/{id}/{id1}', 'WelcomeController#index');
And at welcome.blade.php:
< a href="{{URL::route('/{4}/{5}')}}">test</a>
I want to build a link referencing the route above.
A good approach to do it would be to name your route and then reference it by name, passing the parameters needed. Here is how:
At routes.php:
Route::get('/{id}/{id1}', ['as' => 'welcome_index', 'uses' => 'WelcomeController#index']);
And at your view, you can do this:
test
Notice that the first parameter represent the route name and the second, the parameters for your URL. You can read more here.
The advantages of naming a route is that you can change your route path later and your URL will still work with the code above.

Resources