This is the controller:
public function index()
{
$q = Request::get('q');
$action = Request::get('action');
//$venues = Venue::take(20);
//for drop ordering- would be good if this could be dynamic
$columns = array(
'name' =>'Name',
'client_id' =>'Client',
'contact_no' =>'Contact No.',
'contact_name' =>'Contact Name'
);
if(Request::exists('q') && Request::exists('action')){
//echo $q; exit;
switch($action){
case 'search':
$venues = Venue::search($q)->with('client')->paginate(20);
$title = 'Venues matching "'.$q.'"';
break;
case 'order':
$venues = Venue::orderby($q)->with('client')->paginate(20);
$title = 'Venues matching "'.$q.'"';
break;
default:
$venues = Venue::with('client')->paginate(20);
$title = 'All Venues';
break;
}
} else {
$venues = Venue::with('client')->paginate(20);
$title = 'All Venues';
}
return view('venues.index', compact('title', 'venues','columns'));
}
I am using a scope query to do the ordering:
public function scopeOrderby($query, $value)
{
return $query->orderBy($value, 'DESC');
}
The search query which is fine is this:
public function scopeSearch($query, $value)
{
return $query->where('name', 'LIKE', "%$value%")->orWhere('contact_name','LIKE', "%$value%");
}