dongpa6867 2016-06-07 03:11
浏览 51
已采纳

有没有一种方法可以使用eloquent with model来进行变量查询?

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?


Edit: 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.

  • 写回答

1条回答 默认 最新

  • dsc56927 2016-06-07 03:13
    关注

    for use eloquent just change this line

    $myEntries = new MyTable();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部