Laravel 7中的多条件搜索功能
Multiple condition search function in Laravel 7
我正在尝试搜索具有下拉值和日期范围的值。
但它一次只适用于一个。就像我 select 来自下拉列表的值和 select 日期范围它不显示。
这是我的代码
控制器:
public function sale_index(request $request){
$users = User::orderBy( 'fname', 'asc')->get();
$sales = Sim::where('stock', '>', 0)->orderBy( 'id', 'desc')->get();
if($request->filled('search')){
$sales = Sim::where('user_id', '=', $request->search)->orderBy( 'id', 'desc')->get();
}
if($request->filled('from') AND $request->filled('to')){
$sales = Sim::whereBetween('created_at', [$request->get('from'), $request->get('to')])->orderBy( 'id', 'desc')->get();
}
return view('admin.all_sale', compact('sales','users'));
}
Blade 文件:
<form action="" method="GET">
<select name="search" id="">
<option value="">Seller Name</option>
@foreach ( $users as $row)
<option value="{{ $row->id }}" @if ($row->id == request()->query('search'))
selected
@else
@endif >{{ $row->fname }} {{ $row->lname }}</option>
@endforeach
</select>
<input type="date" name="from">
<input type="date" name="to">
<button type="submit">Search</button>
</form>
请帮我解决这个问题
这两个条件被不同的查询覆盖,而不是将 where
过滤器全部合并为一个查询。
尝试改变这个:
$sales = Sim::where('stock', '>', 0)->orderBy( 'id', 'desc')->get();
if($request->filled('search')){
$sales = Sim::where('user_id', '=', $request->search)->orderBy( 'id', 'desc');
}
if($request->filled('from') AND $request->filled('to')){
$sales = Sim::whereBetween('created_at', [$request->get('from'), $request->get('to')])->orderBy( 'id', 'desc');
}
为此:(更改将 $sales =
替换为 $sales->
到 where
子句)
$sales = Sim::where('stock', '>', 0)->orderBy( 'id', 'desc');
if($request->filled('search')){
$sales->orWhere('user_id', '=', $request->search)->orderBy( 'id', 'desc');
}
if($request->filled('from') AND $request->filled('to')){
$sales->orWhereBetween('created_at', [$request->get('from'), $request->get('to')])->orderBy( 'id', 'desc');
}
$sales->get();
我正在尝试搜索具有下拉值和日期范围的值。 但它一次只适用于一个。就像我 select 来自下拉列表的值和 select 日期范围它不显示。
这是我的代码
控制器:
public function sale_index(request $request){
$users = User::orderBy( 'fname', 'asc')->get();
$sales = Sim::where('stock', '>', 0)->orderBy( 'id', 'desc')->get();
if($request->filled('search')){
$sales = Sim::where('user_id', '=', $request->search)->orderBy( 'id', 'desc')->get();
}
if($request->filled('from') AND $request->filled('to')){
$sales = Sim::whereBetween('created_at', [$request->get('from'), $request->get('to')])->orderBy( 'id', 'desc')->get();
}
return view('admin.all_sale', compact('sales','users'));
}
Blade 文件:
<form action="" method="GET">
<select name="search" id="">
<option value="">Seller Name</option>
@foreach ( $users as $row)
<option value="{{ $row->id }}" @if ($row->id == request()->query('search'))
selected
@else
@endif >{{ $row->fname }} {{ $row->lname }}</option>
@endforeach
</select>
<input type="date" name="from">
<input type="date" name="to">
<button type="submit">Search</button>
</form>
请帮我解决这个问题
这两个条件被不同的查询覆盖,而不是将 where
过滤器全部合并为一个查询。
尝试改变这个:
$sales = Sim::where('stock', '>', 0)->orderBy( 'id', 'desc')->get();
if($request->filled('search')){
$sales = Sim::where('user_id', '=', $request->search)->orderBy( 'id', 'desc');
}
if($request->filled('from') AND $request->filled('to')){
$sales = Sim::whereBetween('created_at', [$request->get('from'), $request->get('to')])->orderBy( 'id', 'desc');
}
为此:(更改将 $sales =
替换为 $sales->
到 where
子句)
$sales = Sim::where('stock', '>', 0)->orderBy( 'id', 'desc');
if($request->filled('search')){
$sales->orWhere('user_id', '=', $request->search)->orderBy( 'id', 'desc');
}
if($request->filled('from') AND $request->filled('to')){
$sales->orWhereBetween('created_at', [$request->get('from'), $request->get('to')])->orderBy( 'id', 'desc');
}
$sales->get();