Reputation: 115
Laravel newbie looking for guidance. Can anyone please show me the best way to add optional posted search parameters to the following Eloquent query and return all related data and paginated? The optional search parameters are the foreign keys; country_id, season_id and parameter_id.
$dataitems = Dataitem::orderBy('name')->with('country')->with('season')->with('parameter')->paginate(10);
I am reading the docs and have seen the fluent query builder I'm just struggling to recreate this nicely paginated query.
Many thanks.
Upvotes: 1
Views: 1958
Reputation: 4576
I am not sure if I understand what you want. But i would do something like this :
Create your base eloquent query :
$query = Dataitem::orderBy('name');
If you have a specific parameter, add the condition to you existant query. You can repeat this if block for all your optional parameters.
if (Input::has('country_id'))
{
$query->whereHas('country', function($q){
$q->where('id', '=' ,Input::get('country_id'));
});
}
When you're done, paginate:
$query->paginate(10);
Upvotes: 3