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']);

4 Answers 4

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') }}
4
  • 13
    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.
    – Joel Hinz
    Commented Mar 29, 2019 at 21:16
  • 2
    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.
    – Ermac
    Commented Nov 25, 2020 at 20:04
  • @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.
    – Joel Hinz
    Commented Nov 26, 2020 at 6:39
  • @JoelHinz I wouldn't call Request::route() logic - it seems pretty standard for Laravel. Commented Jan 10, 2022 at 0:01
37

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

{{dd(request()->route()->parameters)}}
1
  • 1
    This doesn't work; it returns empty. dd(request()->query('testing')) works though.
    – sykez
    Commented Jul 2, 2019 at 8:04
14

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

0
7

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

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.