【问题标题】:Laravel error on logout注销时的 Laravel 错误
【发布时间】:2015-04-19 11:32:16
【问题描述】:

我有一个自定义 AppUserProvider 来从缓存设置中获取我的用户,如下所示:

class AppUserProvider extends \Illuminate\Auth\EloquentUserProvider {

    public function retrieveById($identifier)
    {
        return \App\Models\User::getById($identifier);
    }
}

然后在使用 Eloquent 检索对象并返回它之后,我在 getById 方法中为其设置自定义属性。

$user->test = 'test';

一切正常,但是当我点击注销时出现错误。

update `users` set `test` = ?, `remember_token` = ?, `updated_at` = ? where `id` = ?')

不确定它为什么要更新 test 参数。

【问题讨论】:

  • 这个test 属性是干什么用的?当 Laravel 对模型运行更新时,它会获取所有(更改的)属性并将它们放入查询中。
  • 我实际上将权限从键/值存储到参数以便于访问,例如Auth::user()->permissions->save_file。也许有更好的方法来处理这个。

标签: php laravel eloquent laravel-5


【解决方案1】:

每次当你在 Eloquent 模型上设置一个实际上不存在(未在类中定义)的属性时,__set() 会被调用,Laravel 将值存储为模型属性。然后在运行更新时,Laravel 会注意到您添加了一个新属性并将其添加到更新查询中。

您只需在模型中定义属性即可防止这种情况发生:

public $test;

或者,如果您想像对象一样使用所述属性,则应将其初始化为一个:

public $permissions;

public function __construct(array $attributes = array()){
    $this->permissions = new stdClass;
    parent::__construct($attributes);
}

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2016-04-06
    • 2017-10-23
    • 2014-07-19
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多