模型实例化传入参数丢失
我在实例化模型时传递了一些参数,然后写入到静态变量种,在执行到boot方法时去获取参数,发现参数设置的数据没了。
这是模型实列化代码
$model = new ProductInventorySet([‘userData’ => $user, ‘search’ => $data]);
$list = $model->where(‘del_time’,0)
->select(‘id’,’top_depart_id’,’param_one’,’param_two’)
->orderby(‘id’, ‘desc’)
->get()->toArray();
下面是model的代码
protected $guarded = [];
public static $user = [];
public static $search = [];
public static $is_search = false;
public function construct(array $attributes = [])
{
if(! empty($attributes[‘userData’])) {
self::$user = $attributes[‘userData’];
self::$search = $attributes[‘search’];
self::$is_search = true;
}
parent::construct($attributes);
}
protected static function boot(){
parent::boot();dump(Self::$user);
if(self::$is_search){
static::addGlobalScope(new TopDepartmentScope(self::$user, self::$search));
}
}
最后在boot dump的时候发现$user为空数组
之前删除了该模型 重新建了一个 发现问题解决了
后面又失效了 接受不到参数
1. 运行环境
centos7
1). 当前使用的 Laravel 版本?
6.20.26
[/6/]: <> (使用 php artisan --version
命令查看)
2). 当前使用的 php/php-fpm 版本?
PHP 版本:
php-fpm 版本:
推荐文章: