繁体   English   中英

Laravel 5.5 BelongsToMany与枢轴条件

[英]Laravel 5.5 BelongsToMany with pivot conditions

我有三个型号, ClinicDepartmentClinicDepartment并具有belongsToMany关系称为departmentsClinicDepartment使用ClinicDepartment的表作为数据透视表,但在使用这种关系我,范围从ClinicDepartment没有查询aplying。

我决定制作调用ClinicDepartmentPivot Pivot模型,并将这些范围应用于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模型,因此当我尝试获取Department Clinics时,应检查-ClinicDepartment是否具有is_active = 1且未删除。

UPD 1

我的范围特征如下所示:

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

可以被任何型号使用。

您在这里缺少了一些东西:

要将全局范围分配给模型,应覆盖给定模型的引导方法,并使用addGlobalScope方法

Laravel关于范围的文档

您的模型应如下所示:

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)

谢谢大家的想法。

暂无
暂无

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

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