0

I am getting a true return with very query in Laravel 5.2. I am making query in the controller and returning an array.

 if($term = $request->get('term')){

          $booking = guests::where('booking', '=', $term)->get(); 
          $active =  guests::where('booking', '=', $term)->pluck('active');

      }          
    // dd($active);

      if($active){
          echo '

I have read it could potentially be solved by attribute casting by attempts have not worked.

Thanks

1 Answer 1

3

Both get() and pluck() will return a collection, so making an if condition like the one you are doing - will not return false even if a collection is completely empty (collection method isEmpty() would return false though). The result of if ($active) in your code has nothing to do with the value of the 'active' field itself.

You can try adding first() to the chain, assuming that you only have or you only need one item:

$booking = guests::where('booking', '=', $term)->get()->first();
$active =  guests::where('booking', '=', $term)->pluck('active')->first();
Sign up to request clarification or add additional context in comments.

2 Comments

@Jeff was within the minute, then left my computer. Chill!
it wont return false even if the collection is empty btw, object == true.

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.