Home » Php » php – laravel 5.2 : Get query string

php – laravel 5.2 : Get query string

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

To determining 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:

Answer:

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

Answer:

You can get input by:

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

For above 5.*

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

Or

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

Answer:

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 ✔

Answer:

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

Answer:

To check if invitee parameter exists:

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

To get the value of invitee:

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