Laravel 5.5 BelongsToMany 具有枢轴条件

Laravel 5.5 BelongsToMany with pivot conditions

我有三个模型,ClinicDepartmentClinicDepartment,并且有 belongsToMany 关系 departmentsClinicDepartment 使用 ClinicDepartment 的 table 作为枢轴 table,但是当我使用此关系时,来自 ClinicDepartment 的范围不适用于查询。

我决定调用 Pivot 模型 ClinicDepartmentPivot 并将这些作用域应用于 Pivot,但运气不好。

诊所模式:

class Clinic extends Model
{
    use SoftDeletes, ActiveOnly, HasParent;

    public function departments()
    {
        return $this->belongsToMany(Department::class, 'clinic_departments', 'clinic_id', 'department_id')->using(ClinicDepartmentPivot::class);
    }
}

部门模型:

class Department extends Model
{
    use SoftDeletes, ActiveOnly;

    public function clinics()
    {
        return $this->belongsToMany(Clinic::class, 'clinic_departments', 'department_id', 'clinic_id')->using(ClinicDepartmentPivot::class);
    }
}

ClinicDepartmentPivot 模型:

class ClinicDepartmentPivot extends Pivot
{
    use ActiveOnly, SoftDeletes;
}

ActiveOnlyScope:

class ActiveOnlyScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        $builder->where($model->getTable() . '.is_active', true);
    }
}

所以基本上我想将全局范围应用于 Pivot 模型,所以当我尝试获取部门诊所时,它应该检查 - 诊所部门是否有 is_active = 1 而不是删除。

更新 1

我的范围特征如下所示:

trait ActiveOnly
{
    public static function bootActiveOnly()
    {
        if (!Auth::guard('admin')->check() && strpos(request()->getRequestUri(), 'admin') === false) {
            static::addGlobalScope(new ActiveOnlyScope);
        }
    }
}

任何模型都可以使用。

您在这里遗漏了一些东西:

To assign a global scope to a model, you should override a given model's boot method and use the addGlobalScope method

Laravel Docs on scope

您的模型应如下所示:

class Clinic extends Model
{
    use SoftDeletes, ActiveOnly, HasParent;

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new ActiveOnlyScope);
    }

    public function departments()
    {
        return $this->belongsToMany(Department::class, 'clinic_departments', 'clinic_id', 'department_id')->using(ClinicDepartmentPivot::class);
    }
}



class Department extends Model
{
    use SoftDeletes, ActiveOnly;

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new ActiveOnlyScope);
    }

    public function clinics()
    {
        return $this->belongsToMany(Clinic::class, 'clinic_departments', 'department_id', 'clinic_id')->using(ClinicDepartmentPivot::class);
    }
}

好的,通过一些变通方法,我终于找到了一个看起来不错的可行解决方案。

public function clinics()
    {
        return $this->belongsToMany(Clinic::class, 'clinic_departments', 'department_id', 'clinic_id')
            ->where(function (Builder $query) {
                $query->where('clinic_departments.is_active', 1)
                    ->whereNull('clinic_departments.deleted_at');
            });
    }

实际查询如下所示:

select `clinics`.*, `clinic_departments`.`department_id` as `pivot_department_id`, `clinic_departments`.`clinic_id` as `pivot_clinic_id` from `clinics` inner join `clinic_departments` on `clinics`.`id` = `clinic_departments`.`clinic_id` where (`clinic_departments`.`is_active` = 1 and `clinic_departments`.`deleted_at` is null)

感谢大家的想法。