Lumen: get URL parameter in a Blade view

PhpLaravelBladeLumen

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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/

Solution 2 - Php

The shortest way i have used

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

Solution 3 - Php

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

Solution 4 - Php

More simple in Laravel 5.7 and 5.8

{{ Request()->parameter }}

Solution 5 - Php

This works fine for me:

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

Ex: to get pagination param on blade view:

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

Solution 6 - Php

Laravel 5.8

{{ request()->a }}

Solution 7 - Php

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

Solution 8 - Php

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

Solution 9 - Php

Laravel 5.6:

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

Solution 10 - Php

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

Solution 11 - Php

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreaView Question on Stackoverflow
Solution 1 - PhpAndreaView Answer on Stackoverflow
Solution 2 - PhpHai NguyenView Answer on Stackoverflow
Solution 3 - PhpBrad AhrensView Answer on Stackoverflow
Solution 4 - PhpEcko SantosoView Answer on Stackoverflow
Solution 5 - PhpFred SousaView Answer on Stackoverflow
Solution 6 - Phpb00sted 'snail'View Answer on Stackoverflow
Solution 7 - PhpNik SumeikoView Answer on Stackoverflow
Solution 8 - PhpMaksim IvanovView Answer on Stackoverflow
Solution 9 - PhpAlexander KimView Answer on Stackoverflow
Solution 10 - PhpNapster ScofieldView Answer on Stackoverflow
Solution 11 - PhpRahman RezaeeView Answer on Stackoverflow