laravel多场搜索表单
问题描述:
有一个多字段搜索表单在我的Laravel 5.2的应用程序laravel多场搜索表单
price
rooms
type
rent
我有一些事情我错了没有结果正确,我认为,因为IM使用orWhere()。我想要和输入字段之间。我想为此提供任何解决方案
public function search()
{
$un_price = \Request::get('un_price');
$un_rooms = \Request::get('un_rooms');
$un_type = \Request::get('un_type');
$un_rent = \Request::get('un_rent');
$units = DB::table('units')->whereIn('un_status', [1])
->where('un_price','like','%'.$un_price.'%')
->orWhere("un_rooms", "LIKE", "%$un_rooms%")
->orWhere("un_type", "LIKE", "%$un_type%")
->orWhere("un_rent", "LIKE", "%$un_rent%")
->paginate(20);
return view('home.units.show', compact('units'));
}
答
使用查询字符串过滤。难道是这样的:
public function search(Request $request)
{
$unit = (new Unit)->newQuery(); //where Unit is the model
if($request->has('un_price'){
$unit->where('un_price',$request->un_price)
}
if($request->has('un_rooms'){
$unit->where('un_rooms',$request->un_rooms)
}
//go on until the end
$units = $unit->get();
return view('home.units.show', compact('units'));
}
这种方式,您得到您根据您收到的输入需要的所有结果。
只是用where而不是orWhere呢? –
当我使用哪里只有我什么都没有得到 – fido
concat变量与%标记在orWhere字段喜欢在你的where字段,否则你正在寻找“un_rooms like'%$ un_rooms%'”... –