Lumen: get URL parameter in a Blade view
Asked Answered
E

13

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

Escobar answered 9/7, 2015 at 17:51 Comment(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.Anywhere
@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.Escobar
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...Anywhere
Think about the reason why you can't access $request directly in view, also why accessing $_GET,$_POST,$_REQUEST directly is bad practiceKillifish
@Killifish true, actually. Frameworks remove potentially bad data from these variables.Jabin
E
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/

Escobar answered 17/7, 2015 at 18:47 Comment(3)
this will return all params app('request')->request->all()Hollis
what would be the way if my URL is like locahost:8000/example/10 and I wanna get the value 10. @EscobarWershba
@SajeebAhamed Better you ask this as a new question: stackoverflow.com/questions/askEscobar
S
72

The shortest way i have used

{{ Request::get('a') }}
Surrealism answered 16/8, 2016 at 8:39 Comment(1)
Where is this documented?Uncounted
S
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
Send answered 12/6, 2019 at 8:28 Comment(0)
S
31

More simple in Laravel 5.7 and 5.8

{{ Request()->parameter }}
Slipnoose answered 16/10, 2018 at 8:1 Comment(1)
Works perfectly, thanks!Virginiavirginie
B
12

Laravel 5.8

{{ request()->a }}
Brenna answered 5/6, 2019 at 8:21 Comment(0)
N
11

This works fine for me:

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

Ex: to get pagination param on blade view:

{{ app('request')->input('page') }}
Negotiation answered 23/2, 2016 at 14:23 Comment(0)
D
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

Disgruntle answered 13/6, 2019 at 22:13 Comment(0)
S
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') }}
Streaming answered 12/2, 2016 at 13:33 Comment(1)
Lumen has /config folder?Sanderlin
C
7

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

{{request("a")}}
Crossquestion answered 15/7, 2022 at 18:6 Comment(0)
G
5

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
Gardas answered 31/12, 2020 at 3:1 Comment(0)
S
4

Laravel 5.6:

{{ Request::query('parameter') }}
Sarracenia answered 11/7, 2018 at 6:38 Comment(1)
Please add some explanation.Alrick
S
3

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

{{dd(request()->route()->parameters)}}
Sherrie answered 20/7, 2020 at 4:5 Comment(0)
O
0

here is the code to get filtered data with pagination

$request->input('a')
Outskirts answered 1/2, 2023 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.