I need to add a global scope to a lot of models (I'm putting it into a trait) which will check if \Session::has('admin')
and if it does it will query the 'admins' table, get the 'main_website' this admin belongs to and only show the results that belong to this website.
The problem is that the global scope dies after I start executing another query and I get error 500 with nothing in my logs.
Here is the trait:
trait MainWebsitable {
public static function bootMainWebsitable()
{
static::addGlobalScope(new MainWebsitableScope);
}
public function scopeByWebsiteId($query, $id)
{
return $query->whereHas('mainWebsites', function ($query) use ($id) {
$query->where('id', $id);
});
}
}
Here is the global scope:
class MainWebsitableScope implements Scope {
public function apply(Builder $builder, Model $model) {
if(\Session::has('admin')) {
$adminId = \Session::get('admin');
$admin = Admin::find($adminId);
$website = $admin->mainWebsites->first();
if(isset($website)) {
$builder->byWebsiteId($website->id);
}
}
}
}
I've tried putting dd(\Session::get('admin'))
after first if and it successfully returns the value saved in session. Then I've tried putting dd($admin) after Admin::find($adminId)
and it returns the same 500 error as if I didn't have dd(). I've also tried doing another query (not querying the Admin model, but some other table) in that place and the same thing happens - 500 error with no explanation.
What am I doing wrong?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2imYrum
via IFTTT
Aucun commentaire:
Enregistrer un commentaire