Thursday, 15 May 2014

php - Laravel - Undefined variable if query returns no results -



php - Laravel - Undefined variable if query returns no results -

if query not have results, possible, variable query assigned returns error 'undefined variable $alerts' on line if(!$alerts).

foreach($locations $location) { $alerts = alert::where('location_id', '=', $location) ->lists('id'); } if(!$alerts){ homecoming redirect::to('/users/')->with('message-warning', 'no matches have been found yet.'); }

the $alerts variable not anywhere else within controller.

even if query returns no results, should still able check through if(!$alerts)?

thank help.

you may seek this:

$alerts = alert::wherein('location_id', $locations)->lists('id'); if(!$alerts) { homecoming redirect::to('/users/')->with('message-warning', 'your message!'); }

it's better, 1 query executed.

php laravel laravel-4 eloquent

No comments:

Post a Comment