关于php:laravel添加静态函数boot()后的模型未定义索引

laravel Model Undefined index after add static function boot()

我将此语法添加到模型中:

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
        });
     }
}

在网络上运行良好,但在API中效果不佳,错误:

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

Laravel框架5.8.37

当我删除" if(Auth :: user())"而仅删除" public static function时
boot()"仍然是相同的错误


最后,我找到了解决方法

只给

parent :: boot();

静态功能的第一行