【问题标题】:Laravel 5.5 BelongsToMany with pivot conditionsLaravel 5.5 BelongsToMany 与枢轴条件
【发布时间】:2019-05-31 05:22:43
【问题描述】:

我有三个模型,ClinicDepartmentClinicDepartment,并且使用 ClinicDepartment 的表作为数据透视表,从 ClinicDepartmentbelongsToMany 关系称为 departments,但是当我使用这种关系,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 模型,所以当我尝试获取部门诊所时,它应该检查 - 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);
        }
    }
}

任何型号都可以使用。

【问题讨论】:

    标签: php laravel eloquent laravel-5.5


    【解决方案1】:

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

    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)
    

    感谢大家的意见。

    【讨论】:

      【解决方案2】:

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

      要将全局范围分配给模型,您应该覆盖给定的 模型的启动方法并使用addGlobalScope方法

      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);
          }
      }
      

      【讨论】:

      • 我知道范围,我正在使用 Traits 分配全局范围,ActiveOnly - 将全局范围分配给模型的 Trait。这就像 Laravel 的 SoftDeletes Trait。问题不是关于如何在模型中使用范围,而是如何在 Pivot 模型中使用范围。
      猜你喜欢
      • 1970-01-01
      • 2016-08-13
      • 2018-08-28
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2018-03-25
      相关资源
      最近更新 更多