Laravel:搜索和过滤数据



我想在laravel中多过滤数据,但我显示此错误:

Too few arguments to function IlluminateSupportCollection::get()

请帮我解决这个问题。

public function searchLanding(Request $request)
{
$landings = Landing::all();
if(count($landings) && !is_null($request->title)) {
$landings = $landings->where("name", "LIKE", "%{$request->title}%")->get();
}
if (count($landings) && !is_null($request->start_at)) {
$landings = $landings->where('start_at', '>=', $request->start_at)->get();
}
if (count($landings) && !is_null($request->end_at)) {
$landings = $landings->where('end_at', '<=', $request->end_at)->get();
}
}
public function searchLanding(Request $request)
{
$landings = Landing::query();
if(!is_null($request->title)) {
$landings->orWhere("name", "LIKE", "%{$request->title}%");
}
if (!is_null($request->start_at)) {
$landings->orWhere('start_at', '>=', $request->start_at);
}
if (!is_null($request->end_at)) {
$landings->orWhere('end_at', '<=', $request->end_at);
}
return $landings->get();
}

注意:
当你还在构建查询时,你不应该调用all()get(),只有当你想要得到结果时才调用它们。

当您希望所有条件为真时使用where()
或当您希望其中一个条件为真时使用orWhere()

在上面的例子中,只有一个条件需要为真,例如在titlestart_at之后或end_at之前搜索。

最新更新