Gammer
Gammer

Reputation: 5608

Laravel get query string

I have the following url http://project.su/?invitee=95

first i want to check the invitee in url, if the url have invitee then get the value.

What i have tried (controller) :

if(!empty($request->get('invitee'))){
   $user->invitee = $request->get('invitee');
}

The following code is not working .

I want storing the invitee result(id) in database.

Thanks.

Upvotes: 31

Views: 145601

Answers (6)

Depzor
Depzor

Reputation: 1316

To determine if an input value is present:

if ($request->has('invitee')) {
   $user->invitee = $request->input('invitee');
}

The has method returns true if the value is present and is not an empty string:

Upvotes: 56

Vikram Bhaskaran
Vikram Bhaskaran

Reputation: 417

As far as Laravel 5.7 is concerned, the preferred way to retrieve query params is

if( $request->has('invitee') ) {
    $request->query('invitee');
}

or using the helper function if you don't have access to $request

request()->query('invitee');

Upvotes: 23

Rohan Khude
Rohan Khude

Reputation: 4883

In laravel 5.7, we can also use request()->invitee. By using this, we can get both, URL path parameters and query parameters. Say we have below URL

http://localhost:8000/users/{id}?invitee=95

To read id and invitee, we can use

$id  ✔
$invitee ✖ - Invalid
request()->id ✔
request()->invitee ✔

Upvotes: 10

Alfonz
Alfonz

Reputation: 1020

To check if invitee parameter exists:

if($request->has('invitee')) {
    // ...
}

To get the value of invitee:

$invitee = $request->input('invitee');

Upvotes: 1

Somnath Muluk
Somnath Muluk

Reputation: 57656

You can get input by:

$invitee = Input::get('invitee');

For above 5.*

$invitee = $request->input('invitee');

Or

$invitee = Request::input('invitee');

Upvotes: 5

Alex
Alex

Reputation: 1638

Are you calling $user->save() anywhere? You should call $user->save() to actually persist the data.

You can always check by calling dd($user); right after the second line in you example if you are worried it is not set correctly, this way you can see what attributes are set in the $user object.

Also you can replace !empty($request->get('invitee')) with $request->has('invitee').

Upvotes: 0

Related Questions