Php Laravel将附加参数传递到has中

Php Laravel将附加参数传递到has中,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,假设我在这样一个模型上有一段关系 public function currentInvoices($time = 'current'){ switch ($time) { case 'current': $start = Carbon::create()->startOfMonth()->format('Y-m-d H:i:s'); $end = Carbon::create()-

假设我在这样一个模型上有一段关系

public function currentInvoices($time = 'current'){
        switch ($time) {
            case 'current':
                $start = Carbon::create()->startOfMonth()->format('Y-m-d H:i:s');
                $end = Carbon::create()->endOfMonth()->format('Y-m-d H:i:s');
            break;
            case 'lastMonth':
                $start = Carbon::create()->subMonth()->startOfMonth()->format('Y-m-d H:i:s');
                $end = Carbon::create()->subMonth()->endOfMonth()->format('Y-m-d H:i:s');
            break;
            default:
                $start = Carbon::create()->startOfMonth()->format('Y-m-d H:i:s');
                $end = Carbon::create()->endOfMonth()->format('Y-m-d H:i:s');
        }

        return $this->hasManyThrough(
            'App\Models\Invoices',
            'App\Models\Tenancies\Tenancies',
            'linked_tenant_id', // Foreign key on Tenancies table...
            'tenant_id', // Foreign key on invoices table...
            'id', // Local key on tenants table...
            'id' // Local key on tenancies table...
        )->where('invoices.start_date','>=',$start)->where('invoices.end_date','<=',$end);
    }

是否可以将第二个参数传递给雄辩的
has
函数?如果没有任何关于如何实现这一点的建议,您可以使用where has

$tenantCompany=$tenantCompany->whereHas('currentInvoices',函数($query)使用($start\u date,$end\u date{

$query->where('start\u date','>=',$start\u date)->where('end\u date','我更喜欢这样做

public function invoices(){
        return $this->hasManyThrough(
            'App\Models\Invoices',
            'App\Models\Tenancies\Tenancies',
            'linked_tenant_id', // Foreign key on Tenancies table...
            'tenant_id', // Foreign key on invoices table...
            'id', // Local key on tenants table...
            'id' // Local key on tenancies table...
        );
}

public function currentInvoices(){
    $start = Carbon::create()->startOfMonth()->format('Y-m-d H:i:s');
    $end = Carbon::create()->endOfMonth()->format('Y-m-d H:i:s');

    return $this->invoices()->where('invoices.start_date','>=',$start)
                            ->where('invoices.end_date','<=',$end);
}

public function lastMonthInvoices(){
    $start = Carbon::create()->subMonth()->startOfMonth()->format('Y-m-d H:i:s');
    $end = Carbon::create()->subMonth()->endOfMonth()->format('Y-m-d H:i:s');

    return $this->invoices()->where('invoices.start_date','>=',$start)
                            ->where('invoices.end_date','<=',$end);
}
$tenantCompany = $tenantCompany->has('lastMonthInvoices');

谢谢你的建议,但这不是我真正想要的。我正在寻找一种在我的模型中运行switch语句的方法,因此我需要能够调用
has
eloquent函数并将一个参数传递给它。@S\R你可以创建两个单独的关系或两个单独的查询。但是你不应该将参数传递给rel设置。这不是它应该如何工作。@s\R您的第二种情况是在类中使用一个变量。并为它创建一个设置器,然后一直更改它。但对于MEA来说,这似乎是一个糟糕的解决方案,因为它似乎无法以我想要的方式实现,我刚刚按照建议在控制器中运行了一个where has。谢谢。我就是这样做的y、 但是名称
currentInvoices
对于刀片中的循环必须保持不变。感谢您的建议。
$tenantCompany = $tenantCompany->has('lastMonthInvoices');