繁体   English   中英

Laravel:搜索和过滤数据

[英]Laravel: search and filter data

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

Too few arguments to function Illuminate\Support\Collection::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()

在上面的示例中,只有一个条件需要为真,例如 search 在titleend_at start_at找到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM