Binding Primitives in Laravel Scope class -
i have global scope used few models. here scope class:
<?php namespace app\scopes; use app\models\usermeta; use auth; use illuminate\database\eloquent\builder; use illuminate\database\eloquent\model; use illuminate\database\eloquent\scope; class workplacescope implements scope { public function apply(builder $builder, model $model) { $user_active_workplace = usermeta::where(['user_id' => auth::id(), 'meta_key' => 'active_workplace'])->first(); $builder->where('workplace_id', $user_active_workplace->meta_value); } }
i assigning scope overriding model's boot
method:
protected static function boot() { parent::boot(); static::addglobalscope(new workplacescope); }
the problem make query inside scope , query executed lot of times no reason. thinking of using primitive binding inside service provider, can't figure out how properly. tried adding inside appserviceprovider
:
$this->app->when('app\scopes\workplacescope') ->needs('$user_active_workplace_meta_value') ->give(1);
but don't know how hint service provider workplacescope expects $user_active_workplace_meta_value variable. idea how can this?
Comments
Post a Comment