Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel Model Undefined index after add static function boot()

Tags:

php

laravel

model

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

like image 442
Wauputra Avatar asked Oct 23 '25 16:10

Wauputra


1 Answers

Finnaly, i found the solution

just give

parent::boot();

in first line of statif function

like image 115
Wauputra Avatar answered Oct 25 '25 06:10

Wauputra