128

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

5
  • 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
    Jul 9, 2015 at 17:54
  • @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
    Jul 9, 2015 at 17:57
  • 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
    Jul 9, 2015 at 18:01
  • Think about the reason why you can't access $request directly in view, also why accessing $_GET,$_POST,$_REQUEST directly is bad practice
    – mvladk
    Jul 9, 2015 at 19:39
  • @mvladk true, actually. Frameworks remove potentially bad data from these variables. Jan 17, 2016 at 15:09

13 Answers 13

158

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/

3
72

The shortest way i have used

{{ Request::get('a') }}
1
  • 2
    Where is this documented?
    – feeela
    Dec 8, 2021 at 12:59
53

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
31

More simple in Laravel 5.7 and 5.8

{{ Request()->parameter }}
1
  • Works perfectly, thanks!
    – Kush
    Feb 10, 2022 at 18:57
12

Laravel 5.8

{{ request()->a }}
11

This works fine for me:

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

Ex: to get pagination param on blade view:

{{ app('request')->input('page') }}
9

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

7

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') }}
1
  • Lumen has /config folder?
    – Mahefa
    May 1, 2020 at 12:57
7

All the answers above are correct, but there's a quickier way to do this.

{{request("a")}}
4

Laravel 5.6:

{{ Request::query('parameter') }}
1
  • 1
    Please add some explanation.
    – petezurich
    Jul 11, 2018 at 7:55
4

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
3

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

{{dd(request()->route()->parameters)}}
0

here is the code to get filtered data with pagination

$queryvariable->appends($data)->links('link of new page');

note there

$data = $request->all();

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.