Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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
Php 非从CLI触发的雄辩事件(在Laravel之外)_Php_Eloquent - Fatal编程技术网

Php 非从CLI触发的雄辩事件(在Laravel之外)

Php 非从CLI触发的雄辩事件(在Laravel之外),php,eloquent,Php,Eloquent,我的模型中有以下事件处理程序: <?php namespace App\Model; use Illuminate\Database\Eloquent\Model; class User extends Model { //... public static function boot() { parent::boot(); static::saving(function ($user) { // di

我的模型中有以下事件处理程序:

<?php
namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    //...

    public static function boot()
    {
        parent::boot();

        static::saving(function ($user) {
            // die('inside');
            if (empty($user->username)) {

                $base = strtolower($user->first_name . '.' . $user->last_name);

                do {
                    $username = $base . @$suffix;
                    $duplicate = User::where('username', '=', $username)->first();
                } while($duplicate and $suffix = rand(1000, 9999));

                // return the original/ generated username
                $user->username = $username;
            }
        });
    }
}
web环境和测试都使用相同的代码。如果我注释行
setEventDispatcher
,那么浏览器环境会抛出一个错误,因为事件处理程序没有启动。所以我知道事件调度器正在做它的工作。而不是测试CLI环境。为什么会这样


顺便说一句,我使用的是雄辩的5.3。

当setEventDispatcher启动时,您需要再次调用它

public static function boot(){
    parent::boot();

    static::setEventDispatcher(new \Illuminate\Events\Dispatcher());
    static::saving(function ($model){
        // now you can reference your model
    }
}

当setEventDispatcher启动时,您需要再次调用它

public static function boot(){
    parent::boot();

    static::setEventDispatcher(new \Illuminate\Events\Dispatcher());
    static::saving(function ($model){
        // now you can reference your model
    }
}