Php 拉威尔链式示波器

Php 拉威尔链式示波器,php,laravel,scope,chain,Php,Laravel,Scope,Chain,我从POST表单中收到了一些变量,如下所示: $search = $request->search; $day = $request->day; $month = $request->month; $year = $request->year; $status = $request->status; 事实上,我还有很多,所以我的问题是。我使用这些变量来查询我的数据库,使用范围。我现在的做法是: 链接范围: $appointments = Appointment::

我从POST表单中收到了一些变量,如下所示:

$search = $request->search;
$day = $request->day;
$month = $request->month;
$year = $request->year;
$status = $request->status;
事实上,我还有很多,所以我的问题是。我使用这些变量来查询我的数据库,使用范围。我现在的做法是:

链接范围:

$appointments = Appointment::latest('created_at')->search(search)->day($day)->month($month)->get(); // and so on..
public function scopeSearch($query, $date)
{
    if($search)
    {
        return $query->whereDate('start', '=', $date);
    } else {
        return false;
    }
}
但这仅在我使用以下作用域时有效:

$appointments = Appointment::latest('created_at')->search(search)->day($day)->month($month)->get(); // and so on..
public function scopeSearch($query, $date)
{
    if($search)
    {
        return $query->whereDate('start', '=', $date);
    } else {
        return false;
    }
}
因为使用上面的链接方法,如果没有搜索任何内容,并且我仍然直接返回查询,那么显然没有找到任何结果。我更改了我的范围,如下所示:

public function scopeSearch($query, $value)
{
    return $query->whereDate('condition', '=', $value);
}
但我必须在控制器中执行以下操作:

$appointments = Appointment::latest('created_at');

if($day) {
    $appointments = $appointments->month($day);
}

if($month) {
    $appointments = $appointments->month($month);
}

if($year) {
    $appointments = $appointments->month($year);
}

if($status) {
    $appointments = $appointments->month($status);
}

$appointments = $appointments->get();
是的,这很有效,但这很愚蠢。有没有我还不知道的方法?我应该用foreach来代替它,或者有一种更干净的,拉维的方法吗