Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.5 Blade route() parameter

Can I add a parameter which I can use in a blade template, and which doesn't appear in the url?

route("Home", ['id' => 1]);

@if(isset($id))
    //Do something
@endif
like image 936
s1njar Avatar asked Jan 30 '18 20:01

s1njar


People also ask

How do you get route parameters in blade?

In Laravel 8, you can simply use request()->route('parameter_name') .

What is route parameter in Laravel?

A parameter provided in the route is usually annotated with curly braces. For instance, to pass in a name parameter to a route, it would look like this. Route::get('/params/{name}', function ($name) { return $name }); By convention, the Controller function accepts parameters based on the parameters provided.

What is route in Laravel blade?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.

How can I check my route in Laravel blade?

You can also use Route::current()->getName() to check your route name.


2 Answers

I had need to create route in view and to send parameter that route.

I did it this way:

{{route('test', $id)}}

This article helped me.

like image 131
Nole Avatar answered Sep 19 '22 04:09

Nole


You can pass in parameters just like that yes, but they will be included in the url:

https://laravel.com/docs/5.5/routing#named-routes

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', function ($id) {
    //
})->name('profile');

$url = route('profile', ['id' => 1]);

To pass parameters without including them in the url you will need to add the parameters in the controller/router method, and not withing the route() method. Eg:

Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
like image 26
Andrew Avatar answered Sep 23 '22 04:09

Andrew