我想用查询参数创建一个过滤器,在这里我想在3个地方做,但是如果其中一个不存在,那么就不会有问题,因为它将仅根据过滤器显示,如果存在是没有查询字符串,那么它将显示所有数据
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d')
->where('d.status','=',$request->status)
->orderBy('d.id')
->get();
return response()->json($vendor);
}
答案 0 :(得分:0)
尝试一下
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data')
->where('status','=',$request->status)
->orderBy('id')
->get();
return response()->json($vendor);
}
答案 1 :(得分:0)
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d')
->when($request->status, function ($q, $status) {
return $q->where('d.status','=', $status);
})
->when($request->status_two, function ($q, $status_two) {
return $q->where('d.status_two','=', $status_two);
})
->orderBy('d.id')
->get();
return response()->json($vendor);
}
答案 2 :(得分:0)
作为参考,确切的代码可能对您不起作用。
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d');
if (!empty($request->status_one)) {
$vendor = $vendor->where('d.status','=', $request->status_one);
}
if (!empty($request->status_two)) {
$vendor = $vendor->where('d.status','=', $request->status_two);
}
if (!empty($request->status_three)) {
$vendor = $vendor->where('d.status','=', $request->status_three);
}
if (empty($request->status_one) && empty($request->status_two) && empty($request->status_three)) {
$vendor= $vendor->where('d.status','=', $request->status_one)->where('d.status','=', $request->status_two)->where('d.status','=', $request->status_three);
}
$result = $vendor->orderBy('d.id')
->get();
return response()->json($result);
}