Skip to content

Instantly share code, notes, and snippets.

@tdchien
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tdchien/e30779e928c419975993 to your computer and use it in GitHub Desktop.
Save tdchien/e30779e928c419975993 to your computer and use it in GitHub Desktop.
Eloquent Model Event
// Database connector
<?php
use Illuminate\Database\Capsule\Manager as Capsule;
/**
* Configure the database and boot Eloquent
*/
global $capsule;
$capsule = new Capsule;
$capsule->addConnection(array(
'driver' => getenv('DB_DRIVER'),
'host' => getenv('DB_HOST'),
'database' => getenv('DB_DATABASE'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => getenv('DB_CHARSET'),
'collation' => getenv('DB_COLLATION'),
'prefix' => getenv('DB_PREFIX'),
));
$capsule->setAsGlobal();
$capsule->bootEloquent();
// Test Model
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
class Test extends Eloquent {
protected $table = 'test';
protected $hidden = array();
protected $fillable = array('id', 'title');
protected $nullable = [];
public $timestamps = false;
public static function boot() {
parent::boot();
parent::setEventDispatcher(new Dispatcher());
static::creating(function($test){
write_log('model event creating', 'model_event');
});
}
}
// Test Controller
//$test = Test::create(['title' => 'test']);
//$test = Test::insert(['title' => 'test']);
$test = new Test;
$test->title = 'test';
$test->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment