0
votes

i add this styntax to model :

public static function boot()
{
   if(Auth::user()){
        parent::boot();
        static::saving(function ($model) {
            $model->school_id = Auth::user()->school_id;
        });
        
        static::addGlobalScope('school_id', function (Builder $builder) {
            $builder->where('school_id', Auth::user()->school_id);
        });
     } 
}

goes well on the web, but not in API, error :

ErrorException: Undefined index: App\SmStudent in file /home/simsmar/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 241

.

Laravel Framework 5.8.37

when i delete "if(Auth::user())" and only "public static function boot()" it's still same error

1
May be you are logged in, on API but not logged in, on web. Are you checking the authenticate user with if(Auth::user())? then you can check like that if(Auth::check()) - sta
but when i delete "if(Auth::user())" and only "public static function boot()" it's still same error - Wauputra
Can you please current post your current laravle version ? So I can help you. - NikuNj Rathod
Laravel Framework 5.8.37 - Wauputra
Hi, what is the line that causes the exception? - loic.lopez

1 Answers

4
votes

Finnaly, i found the solution

just give

parent::boot();

in first line of statif function