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
}
}