Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 为什么我的战靴不叫?_Laravel - Fatal编程技术网

Laravel 为什么我的战靴不叫?

Laravel 为什么我的战靴不叫?,laravel,Laravel,在phpunit测试中,我正在更新值: /** @test */ public function it_updates_newsletter_when_user_privacy_is_updated() { $privacy = create('App\UserPrivacy', ['user_id' => auth()->id()]); $privacy->update(['firstname' => 'Philipp']); } 我试图用一种

在phpunit测试中,我正在更新值:

/** @test */
public function it_updates_newsletter_when_user_privacy_is_updated() 
{

    $privacy = create('App\UserPrivacy', ['user_id' => auth()->id()]);

    $privacy->update(['firstname' => 'Philipp']);

}
我试图用一种特质来捕捉最新的事件。但问题是,它从来没有被叫来过。这是一个特点:

trait UpdatesNewsletter
{
    /**
     * Boot the trait.
     */
    protected static function bootUpdatesNewsletter()
    {
        if (auth()->guest()) return;

        static::updated(function ($model){
            var_dump('is called by trait');
        });

    }
}
为了进行理智检查,我为模型本身编写了事件捕获

class UserPrivacy extends Model
{
    use UpdatesNewsletter;

     /**
      * Boot the model.
      */
    protected static function boot()
    {
        parent::boot();

        static::updated(function ($privacy){
            var_dump('is called by class');
        });
    }
}
在我的控制台中,我看到了唯一的信息:

string(18) "is called by class"

我做错了什么?

这很奇怪,不知何故我没有auth会话,只有在这种情况下

protected static function bootUpdatesNewsletter()
{
    static::updated(function ($model){
        if (auth()->guest()) return;   // at this point you have access to auth()->user etc
        var_dump('is called by trait');
    });

}

你能试着公开trait函数吗?我猜
auth()->guest()
正在返回
true
。在签入trait的引导方法之前,放置一个
dd(auth()->guest())