0

I have this code:

$u_t = User::where('set', '1')->get();

When I die and dump dd($u_t) I get the following:

  Collection {#159 ▼
  #items: array:1 [▼
    0 => set {#158 ▶}
  ]
}

This is good so far. Now when I replace the "1" with some gibberish, like so: $u_t = User::where('set', 'adsfljasdlkf')->get();

I get:

 Collection {#164 ▼
  #items: []
}

I don't really get null. How can I return null?

Edit: when I do ->first() instead of ->get(), I get null if I enter junk. I don't get null if I replace it with ->get().

1 Answer 1

2

When using get(), you're getting a collection of users, so it makes no sense for it to return null. It always returns a collection, regardless of how many users it found.

If you insist, you can change it to null yourself:

$users = User::where('set', '1')->get();

if ($users->isEmpty()) {
    $users = null;
}
Sign up to request clarification or add additional context in comments.

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.