在我的Laravel 5.3项目中,我有一个带有一个文本框(关键字)和两个选择下拉框(类别和位置)的表单。我试图在我的控制器中构建select子句,但是当我为table_1.category或table_1.location设置where时,它就不起作用了。
$keys = \Request::get('keyword');
$category = \Request::get('category');
$location = \Request::get('location');
$myrecord = mytable_1::join('table_2', 'table_2.user_id', '=', 'table_1.user_id')
->where(function ($query) {
$keys = \Request::get('keyword');
$query->where('table_1.title', 'like','%'.$keys.'%')
->orWhere('table_2.company', 'like','%'.$keys.'%');
})
->where('table_1.category', $category)
->where('table_1.location', $location)
->select('table_2.*', 'table_1.*')
->paginate(30);请告诉我实现这一目标的方法。谢谢
发布于 2017-02-27 13:02:50
你需要使用' use ()‘方法将一个参数传递给你的where query...if,你的参数包含数组,然后你可以使用foreach循环。我想它会起作用的..。
$keys = \Request::get('keyword');
$category = \Request::get('category');
$location = \Request::get('location');
$myrecord = mytable_1::join('table_2', 'table_2.user_id', '=','table_1.user_id')
->where(function($query) use ($keys){
$query->where('table_1.title', 'like','%'.$keys.'%')
->orWhere('table_2.company', 'like','%'.$keys.'%');
})
->where('table_1.category', $category)
->where('table_1.location', $location)
->select('table_2.*', 'table_1.*')
->paginate(30);发布于 2017-02-27 12:25:31
如果您使用的是Laravel约定,那么我希望列名应该是table_1.category_id和table_1.location_id。如果$category和$location是这些记录的整数If,那么它应该按照您期望的方式工作。
https://stackoverflow.com/questions/42477487
复制相似问题