cytsunny cytsunny - 9 months ago 45
PHP Question

Is there a way I can use eloquent with model to make variable query?

I have a HTML form for inputting filters for information of my database. Without any input from the form, I would like to show all the records. I have already able to done that using eloquent alone.

//.... codes of my corresponding controller function ......
$inputs = Input::all();
$myEntries = DB::table('myTable'); //what should this line be when using model?
if (!empty($inputs['query_name']))
$myEntries = $myEntries->where('name', $inputs['query_name']);
if (!empty($inputs['min_age']))
$myEntries = $myEntries->where('age', '>', $inputs['min_age']);
$myEntries = $myEntries->paginate(10);
//..... some other codes before returning $myEntries to view .......

If I want to use model, what should be the target line be so I can do similar things?

I was using laravel 4.2 when I asked this question, but recently I have just used this in laravel 5.3 and it works as well. So, I have removed the version specification in question.

Answer Source

for use eloquent just change this line

$myEntries = new MyTable();