Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.2 How to get route parameter in blade?

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']);
like image 490
msonowal Avatar asked Aug 18 '16 06:08

msonowal


People also ask

How do you get the route in Laravel blade?

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

What is route parameter in Laravel?

Laravel routes are located in the app/Http/routes.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. By convention, the Controller function accepts parameters based on the parameters provided.

Where are routes stored in Laravel?

The Default Route Files 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 to get the parameter of a route in Laravel?

@JoelHinz I wouldn't call Request::route () logic - it seems pretty standard for Laravel. If you want to get the parameters without using the controller method This doesn't work; it returns empty. dd (request ()->query ('testing')) works though. In Laravel 8, you can simply use request ()->route ('parameter_name').

How do I inject a Laravel model into a route?

When injecting a model ID to a route or controller action, you will often query the database to retrieve the model that corresponds to that ID. Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes.

What is route model binding in Laravel?

Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes. For example, instead of injecting a user's ID, you can inject the entire User model instance that matches the given ID.

How to get route parameters value in middleware?

Sometimes we may require to get route parameters value in our middleware like if you want to check permission etc. You can get easily using request object, that provide route method and you can get it. I also added small example that way you can undestand very well.


4 Answers

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') }}
like image 141
Joel Hinz Avatar answered Oct 19 '22 10:10

Joel Hinz


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

{{dd(request()->route()->parameters)}}
like image 37
Ivan Z. Avatar answered Oct 19 '22 11:10

Ivan Z.


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

like image 27
Jakub Adamec Avatar answered Oct 19 '22 10:10

Jakub Adamec


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

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

like image 38
Rahman Rezaee Avatar answered Oct 19 '22 10:10

Rahman Rezaee