94
votes

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)?

11
You could access the $_GET[] array, but I wouldn't recommend doing that. You should pass it from the controller to the view, not sure why you woulnd't want to. - Tim Lewis
@TimLewis I would avoid to pass it from the controller because if I have a lot of parameters it could be annoying, and it should be more quick having a way to get it directly from the view. - Andrea
I guess it can be tedious to define and pass a lot of variables from the controller to the view, but I would take tediousness over bad practices any day... - Tim Lewis
Think about the reason why you can't access $request directly in view, also why accessing $_GET,$_POST,$_REQUEST directly is bad practice - mvladk
@mvladk true, actually. Frameworks remove potentially bad data from these variables. - Bhargav Nanekalva

11 Answers

123
votes

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/

62
votes

The shortest way i have used

{{ Request::get('a') }}
32
votes

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! :)

24
votes

More simple in Laravel 5.7 and 5.8

{{ Request()->parameter }}
9
votes

This works fine for me:

{{ app('request')->input('a') }}

Ex: to get pagination param on blade view:

{{ app('request')->input('page') }}
8
votes

Laravel 5.8

{{ request()->a }}
7
votes

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') }}
7
votes

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

Docs

4
votes

Laravel 5.6:

{{ Request::query('parameter') }}
2
votes

if you use route and pass paramater use this code in your blade file

{{dd(request()->route()->parameters)}}
1
votes

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