Php Laravel 5.5属于枢轴条件下的气孔
我有三个模型,Php Laravel 5.5属于枢轴条件下的气孔,php,laravel,eloquent,laravel-5.5,Php,Laravel,Eloquent,Laravel 5.5,我有三个模型,Clinic、Department和ClinicDepartment,并且使用ClinicDepartment的表作为透视表,从ClinicDepartment到Department有belongstomy关系,称为departments,但是当我使用这个关系时,来自临床科室的范围不适用于查询 我决定让Pivot模型调用ClinicDepartmentPivot,并将这些作用域应用于Pivot,但运气不好 诊所模式: class Clinic extends Model {
Clinic
、Department
和ClinicDepartment
,并且使用ClinicDepartment
的表作为透视表,从ClinicDepartment
到Department
有belongstomy
关系,称为departments
,但是当我使用这个关系时,来自临床科室的范围
不适用于查询
我决定让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);
}
}
临床科室枢轴模型:
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);
}
}
所以基本上我想将全局作用域应用于透视模型,所以当我尝试获取科室的诊所时,它应该检查-is ClinicDepartment hasis_active=1
,并且没有删除
UPD 1
我的范围特征如下所示:
trait ActiveOnly
{
public static function bootActiveOnly()
{
if (!Auth::guard('admin')->check() && strpos(request()->getRequestUri(), 'admin') === false) {
static::addGlobalScope(new ActiveOnlyScope);
}
}
}
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)
可供任何型号使用。您缺少以下内容: 若要将全局范围指定给模型,应重写给定的 模型的引导方法,并使用
addGlobalScope
方法
您的模型应如下所示:
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);
}
}
好的,通过一些变通方法,我终于找到了一个看起来不那么零碎的解决方案) 实际查询如下所示:
trait ActiveOnly
{
public static function bootActiveOnly()
{
if (!Auth::guard('admin')->check() && strpos(request()->getRequestUri(), 'admin') === false) {
static::addGlobalScope(new ActiveOnlyScope);
}
}
}
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)
谢谢大家的想法。我知道范围,我正在使用Trait来分配全局范围,
ActiveOnly
-将全局范围分配给模型的Trait。这就像拉威尔的性格。问题不在于如何在模型中使用作用域,而在于如何在透视模型中使用作用域。