laravel Model Undefined index after add static function boot()

Viewed 972

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 Answers

Finnaly, i found the solution

just give

parent::boot();

in first line of statif function

Related