I'm trying to get a url parameter from a view file.
I have this url:
http://locahost:8000/example?a=10
and a view file named example.blade.php
.
From the controller I can get the parameter a
with $request->input('a')
.
Is there a way to get such parameter from the view (without having to pass it from the controller to the view)?
This works fine for me:
{{ app('request')->input('a') }}
Ex: to get pagination param on blade view:
{{ app('request')->input('page') }}
Laravel 5.6:
{{ Request::query('parameter') }}
The shortest way i have used
{{ Request::get('a') }}
This works well:
{{ app('request')->input('a') }}
Where a
is the url parameter.
See more here: http://blog.netgloo.com/2015/07/17/lumen-getting-current-url-parameter-within-a-blade-view/
As per official 5.8 docs:
The request() function returns the current request instance or obtains an input item:
$request = request();
$value = request('key', $default);
Given your URL:
http://locahost:8000/example?a=10
The best way that I have found to get the value for 'a' and display it on the page is to use the following:
{{ request()->get('a') }}
However, if you want to use it within an if statement, you could use:
@if( request()->get('a') )
<script>console.log('hello')</script>
@endif
Hope that helps someone! :)
More simple in Laravel 5.7 and 5.8
{{ Request()->parameter }}
if you use route and pass paramater use this code in your blade file
{{dd(request()->route()->parameters)}}
Laravel 5.8
{{ request()->a }}
As per official documentation 8.x
We use the helper request
The request function returns the current request instance or obtains an input field's value from the current request:
$request = request();
$value = request('key', $default);
the value of request is an array you can simply retrieve your input using the input key as follow
$id = request()->id; //for http://locahost:8000/example?id=10
You can publicly expose Input
facade via an alias in config/app.php
:
'aliases' => [
...
'Input' => Illuminate\Support\Facades\Input::class,
]
And access url $_GET
parameter values using the facade directly inside Blade view/template:
{{ Input::get('a') }}
Source: Stackoverflow.com