James Parsons James Parsons - 2 years ago 84
PHP Question

Bool is returning an array causing true response every time in Laravel 5.2

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

echo '

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


Answer Source

Both get() and pluck() will return a collection, so making an if condition like the one you are doing - will not return false unless a collection is completely empty. It will have 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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download