laravel 5.2 How to get route parameter in blade?
Asked Answered
D

4

47

this is my url http://project.dev/blogs/image-with-article so, here I need the parameter image-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']);
Doityourself answered 18/8, 2016 at 6:51 Comment(0)
B
87

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') }}
Bulldog answered 18/8, 2016 at 7:0 Comment(4)
I 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.Bulldog
Hi, 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.Essene
@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.Bulldog
@JoelHinz I wouldn't call Request::route() logic - it seems pretty standard for Laravel.Jinny
N
37

If you want to get the parameters without using the controller method

{{dd(request()->route()->parameters)}}
Narrative answered 14/6, 2017 at 11:44 Comment(1)
This doesn't work; it returns empty. dd(request()->query('testing')) works though.Monovalent
S
14

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

Scoter answered 6/10, 2020 at 13:7 Comment(0)
D
7

Easy way Just {{ dd(request()->query("PARAMNAME")) }}

for get all PARAMs {{ dd(request()->query()) }}

Delict answered 1/2, 2021 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.