Framework: Observers not firing when using Capsule

Created on 9 Feb 2015  路  6Comments  路  Source: laravel/framework

Hey there,

I'm using illimunate/database with Slim Php and everything is working great except for observers. I've been migrating a project over from laravel and the observers work there, just not in slim php..

My config:

$capsule = new Capsule; $capsule->addConnection(array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'xxx', 'username' => 'xxx', 'password' => 'xxx', 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => '', 'options' => array( PDO::ATTR_CASE => PDO::CASE_LOWER ), ), 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent();

Observer:

class UserObserver { public function saved($model) { //do something } } //register User::observe(new UserObserver);

This never gets run on a save. Any ideas?

Cheers,
Ben

Most helpful comment

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

Read the doc https://github.com/illuminate/database

You should include composer require "illuminate/events" (with relevant version constraint).

All 6 comments

Have you attach the event dispatcher?

I don't think so, any ideas on how to do that?

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

Read the doc https://github.com/illuminate/database

You should include composer require "illuminate/events" (with relevant version constraint).

Thanks for the reply, however I get the error:

PHP Fatal error:  Class 'Illuminate\\Events\\Dispatcher' not found

When i try that :(

composer require "illuminate/events"

many thanks it's working great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shopblocks picture shopblocks  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

kerbylav picture kerbylav  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments