laravel 5.2 How to get route parameter in blade?

97,252

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()) }}

Share:
97,252
msonowal
Author by

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, 2022

Comments

  • msonowal
    msonowal almost 2 years

    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']);
    
  • Joel Hinz
    Joel Hinz about 5 years
    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.
  • sykez
    sykez almost 5 years
    This doesn't work; it returns empty. dd(request()->query('testing')) works though.
  • beluga
    beluga over 3 years
    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.
  • Joel Hinz
    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
    Hashim Aziz over 2 years
    @JoelHinz I wouldn't call Request::route() logic - it seems pretty standard for Laravel.