4

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

5
  • 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()) Commented Aug 15, 2020 at 7:41
  • but when i delete "if(Auth::user())" and only "public static function boot()" it's still same error Commented Aug 15, 2020 at 7:53
  • Can you please current post your current laravle version ? So I can help you. Commented Aug 15, 2020 at 8:19
  • Laravel Framework 5.8.37 Commented Aug 15, 2020 at 8:24
  • Hi, what is the line that causes the exception? Commented Aug 15, 2020 at 12:02

1 Answer 1

16

Finnaly, i found the solution

just give

parent::boot();

in first line of statif function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.