개념 문제 접촉 :의 touches
속성을 사용하여 자동으로 따라 모델에 타임 스탬프를 업데이트 할 때 나는 매우 간단한 문제가 을; 올바르게 작동하지만 전역 범위를 적용합니다.사용 Laravel 글로벌 스코프없이
이 기능을 사용 중지 할 수있는 방법이 있습니까? 아니면 touches
글로벌 범위를 무시하도록 구체적으로 요청 하시겠습니까?
예 콘크리트 : 성분 모델이 업데이트 될 때 모든 관련 요리법 를 접촉해야한다. 로케일을 기반으로 조리법을 분리하기 위해 globalScope
을 가지고 있다는 점을 제외하면 잘 작동합니다. 또한 터치를 적용 할 때 사용됩니다.
성분 모델 :
class Ingredient extends Model
{
protected $touches = ['recipes'];
public function recipes() {
return $this->belongsToMany(Recipe::class);
}
}
레시피 모델 :
class Recipe extends Model
{
protected static function boot()
{
parent::boot();
static::addGlobalScope(new LocaleScope);
}
public function ingredients()
{
return $this->hasMany(Ingredient::class);
}
}
로케일 범위 :
명시 적으로 특정 쿼리에 대한 세계적인 범위를 피하려면class LocaleScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$locale = app(Locale::class);
return $builder->where('locale', '=', $locale->getLocale());
}
}
()'방식을 새로운 관계를 만들 , '터치'는 Laravel의 '$ touch.'속성을 통해 자동으로 호출됩니다. –
나쁜 것, 업데이트 된 답변보기 –