php - Laravel chaining scopes -


i have few variables receiving post form, so:

$search = $request->search; $day = $request->day; $month = $request->month; $year = $request->year; $status = $request->status; 

in reality have bunch more, hence question. use these variables query database, using scopes. way doing now:

chaining scopes:

$appointments = appointment::latest('created_at')->search(search)->day($day)->month($month)->get(); // , on.. 

but works if use following scopes:

public function scopesearch($query, $date) {     if($search)     {         return $query->wheredate('start', '=', $date);     } else {         return false;     } } 

because above chaining method, if nothing searched for, , still directly return query, no results found, obviously. changed scopes this:

public function scopesearch($query, $value) {     return $query->wheredate('condition', '=', $value); } 

but have following in controller:

$appointments = appointment::latest('created_at');  if($day) {     $appointments = $appointments->month($day); }  if($month) {     $appointments = $appointments->month($month); }  if($year) {     $appointments = $appointments->month($year); }  if($status) {     $appointments = $appointments->month($status); }  $appointments = $appointments->get(); 

yes, works, silly. there way of doing not aware of yet? should using foreach instead perhaps or there cleaner, laravel way?


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -