123

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

Andrea
  • 15,900
  • 18
  • 65
  • 84
  • 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 09 '15 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 09 '15 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 09 '15 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 09 '15 at 19:39
  • @mvladk true, actually. Frameworks remove potentially bad data from these variables. – Bhargav Nanekalva Jan 17 '16 at 15:09

13 Answers13

155

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/

Andrea
  • 15,900
  • 18
  • 65
  • 84
71

The shortest way i have used

{{ Request::get('a') }}
Andrea
  • 15,900
  • 18
  • 65
  • 84
Hai Nguyen
  • 907
  • 7
  • 5
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
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
30

More simple in Laravel 5.7 and 5.8

{{ Request()->parameter }}
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
Ecko Santoso
  • 500
  • 4
  • 10
12

Laravel 5.8

{{ request()->a }}
b00sted 'snail'
  • 354
  • 1
  • 13
  • 27
10

This works fine for me:

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

Ex: to get pagination param on blade view:

{{ app('request')->input('page') }}
Fred Sousa
  • 1,121
  • 13
  • 17
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

Maksim Ivanov
  • 3,991
  • 31
  • 25
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') }}
Nik Sumeiko
  • 8,263
  • 8
  • 50
  • 53
7

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

{{request("a")}}
ropehapi
  • 89
  • 1
  • 3
4

Laravel 5.6:

{{ Request::query('parameter') }}
Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
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
Napster Scofield
  • 1,271
  • 14
  • 13
3

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

{{dd(request()->route()->parameters)}}
Rahman Rezaee
  • 1,943
  • 16
  • 24
0

here is the code to get filtered data with pagination

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

note there

$data = $request->all();