Dev.W
Dev.W

Reputation: 2370

Laravel \Request::get()

Is there a way to check if any GET variables exist? I'm not trying to find a particular GET variable I need to create an if statement if ANY get variable exists.

For example, I have a route

/product

If for any reason someone adds variables to the end of the route I'd like to include a canonical link in the header of the page.

<link rel="canonical" href="{{ url($page->slug) }}" />

I can apply this canonical link if I know the parameter. For example if some one adds

/product?model=1

However if someone adds a get variable I'm unaware of then this wouldn't include the link.

I've tried

@if(/Request::get())
   <link rel="canonical" href="{{ url($page->slug) }}" />
@endif

However this squawks with

Too few arguments to function

Upvotes: 1

Views: 1352

Answers (4)

Sohel0415
Sohel0415

Reputation: 9853

If you want to check url contains params or not, then you can use this-

if($request->input()){
   ///do your task
}

And in blade check like-

@if(\Request::input())

@endif

or if you want to get all keys then use $key => $value notation in foreach:

$keys = array();
foreach ($request->input()  as $key => $part) {
   $keys[] = $key;
}

$keys will contain all parameter or input field

Upvotes: 0

Niklesh Raut
Niklesh Raut

Reputation: 34914

You can use \Request::query() to get url params in laravel blade

Try to check all params with this

<?php print_r(\Request::query()); ?>

And apply this code to check any param exist in url

@if(\Request::query())

Check details in laravel doc

Upvotes: 2

Amarnasan
Amarnasan

Reputation: 15529

Just get all the $_GET variables through Input::get():

$parameters= \Input::get()

Upvotes: 0

Dev.W
Dev.W

Reputation: 2370

I managed to do this by counting $_GET. for example:

@if(count($_GET) > 0)
    <link rel="canonical" href="{{ Request::url() }}" />
@endif

Upvotes: 0

Related Questions