Last active
August 29, 2015 14:17
-
-
Save tdchien/e30779e928c419975993 to your computer and use it in GitHub Desktop.
Eloquent Model Event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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