34

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.

1
  • 1
    You should use $_GET['invitee'] because this is a query string not part of the Laravel routing. Commented Aug 3, 2016 at 7:32

7 Answers 7

56

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:

Sign up to request clarification or add additional context in comments.

5 Comments

This only works for URL parameters not for query string.
I completely disagree @TheFallen
This method work for me on query string. Btw thanks @Depzor :)
This method also worked for me on query string LARAVEL 8
this method works perfect for query string, thanks @Depzor
25

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

1 Comment

11

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 ✔

Comments

5

You can get input by:

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

For above 5.*

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

Or

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

Comments

1

To check if invitee parameter exists:

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

To get the value of invitee:

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

Comments

0

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

Comments

0

Since so many different answers have accumulated here, let me clear up the confusion.

->has(), ->query(), ->input(),...

What do we need to check for a query param and get its value?

The clean minimal solution

$value = $request->query('name');
if ($value) {
    // work with the value
}

As stated in Laravel's official documentation section Retrieving Input From the Query String the $request->query('name') is specifically designed to "retrieve values from the query string".

If the method returns null, the param wasn't passed at all or without a value. So with the if condition, we ensure an actual $value.

The all-rounder solution

$request->input() is similar to $request->query(), but it's more general. It does not only work for checking query strings, but also for form data sent via POST, JSON payloads, XHR requests,...

$request->has() checks the sources that the ->input() method draws from.

So this solution works great, too:

if($request->has('name')) {
    $value = $request->input('name');
    // work with the value
}

It works not only for GET requests with query params, but a lot more.

Regarding the OP's code

You're officially discouraged to use the $request->get() method:

This method belongs to Symfony HttpFoundation and is not usually needed when using Laravel.
Instead, you may use the "input" method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.