use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
作曲家,当你需要使用观察家雄辩的要求“亮/事件”要求。
您需要在这样的外部项目中设置Eloquent以供事件和观察者工作。
设置活动:
class Something extends Model {
public static function boot() {
parent::boot();
static::creating(function($item) {
$item->uuid = uuid_generate();
});
}
}
class Something extends Model {
protected $events = [ 'created' => createdEvent::class]
}
检查文档的详细,但两种方式在洋洋洒洒5.4仍然有效。
应该可能工作,但首选的方式已经改变。看看这里的文档:https://laravel.com/docs/5.4/eloquent#events –
事件将被解雇,但你将需要注册一个调度员,实际上做这些事件的事情,因为你不是在Laravel中使用雄辩。 –
上面的代码不起作用,有没有办法做到这一点,并保持简单?我需要写这个调度员? –