laravel 5.2 How to get route parameter in blade?
Solution 1
I'm not sure what you mean. If you're trying to construct the route in a Blade template, use
<a href="{{ route('blog.by.slug', ['slug' => 'someslug']) }}">...</a>
If you're trying to access the given parameter, I would suggest passing it from the controller:
// CmsController
public function show($slug)
{
// other stuff here
return view('someview', compact('slug'));
}
// someview.blade.php
{{ $slug }}
And if you really need to access the parameter from the view without first sending it from the controller... you really shouldn't, but you can use the facade:
{{ Request::route('slug') }}
Solution 2
If you want to get the parameters without using the controller method
{{dd(request()->route()->parameters)}}
Solution 3
In Laravel 8, you can simply use request()->route('parameter_name')
.
Solution 4
Easy way Just
{{ dd(request()->query("PARAMNAME")) }}
for get all PARAMs
{{ dd(request()->query()) }}
msonowal
Snr. Software Developer developer @clarity. I primarily work in PHP and JS and contribute to the open-source projects. A avid gamer likes to play COD and Dirt. I love the Laravel PHP framework and Vue JS.
Updated on July 09, 2022Comments
-
msonowal almost 2 years
this is my url
http://project.dev/blogs/image-with-article
so, here I need the parameterimage-with-article
in my blade to display which is a parameter named slug here is in my routes file I need the slug paramter in blade.Route::get('/blogs/{slug}', ['as'=>'blog.by.slug', 'uses'=> 'CmsController@show']);
-
Joel Hinz about 5 yearsI would like to give myself a shoutout for searching for how to do this only to find my two year younger self telling me it's a stupid idea.
-
sykez almost 5 yearsThis doesn't work; it returns empty.
dd(request()->query('testing'))
works though. -
beluga over 3 yearsHi, why would your last solution be a bad practice? I see it as a good shortcut in the template to not unnecessary occupy the array of variables passed to it.
-
Joel Hinz over 3 years@ankabot I generally want to keep any logic outside of views and just pass them anything they need. It's not a strict rule and it's up to everyone what they prefer, but it's generally considered a best practice.
-
Hashim Aziz over 2 years@JoelHinz I wouldn't call
Request::route()
logic - it seems pretty standard for Laravel.