Skip to content

Instantly share code, notes, and snippets.

@mass6
Last active November 6, 2016 12:29
Show Gist options
  • Save mass6/258c9546ff67fbbea12a296632b26f74 to your computer and use it in GitHub Desktop.
Save mass6/258c9546ff67fbbea12a296632b26f74 to your computer and use it in GitHub Desktop.
Eloquent Boot Sequence

Eloquent Boot Sequeence

Demonstrates Laravel's eloquent model boot sequence, including object construction. #

Order of code executed:

  • Static boot method
public static boot(){...}
  • Trait boot methods
public bootFooTrait(){...}
  • Constructor
public function __construct(){...}

Order of model events

  • Saving
  • Creating (or Updating)
  • Created ( or Updated)
  • Saved
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class History extends Model
{
/**
* The attributes that are not mass assignable.
*
* @var array
*/
protected $guarded = [];
}
<?php
namespace App;
use Illuminate\Support\Facades\Log;
trait LoggingTrait
{
/**
* Boot the logging for the model.
*
* @return void
*/
public static function bootLoggingTrait()
{
Log::info('booting traits');
static::log('booting traits');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use LoggingTrait;
/**
* The attributes that are not mass assignable.
*
* @var array
*/
protected $guarded = [];
public static function boot()
{
self::log('booting');
parent::boot();
static::saving(function ($model) {
$model->log('saving');
});
static::creating(function ($model) {
$model->log('creating');
});
static::created(function ($model) {
$model->log('created');
});
static::updating(function ($model) {
$model->log('updating');
});
static::updated(function ($model) {
$model->log('updated');
});
static::saved(function ($model) {
$model->log('saved');
});
}
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
self::log('constructing');
}
public static function log($event)
{
$row = History::get()->last();
$log = json($event, $row->log);
$row->update(['log' => $log]);
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->text('body')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateHistoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('histories', function (Blueprint $table) {
$table->increments('id');
$table->text('log')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('histories');
}
}
Route::get('/new', function () {
History::create([]);
new Post();
return History::get()->last()->log;
});
Route::get('/new-instantiate', function () {
History::create([]);
$post = new Post(['body' => 'new-instantiate']);
$post->save();
return History::get()->last()->log;
});
Route::get('/create', function () {
History::create([]);
Post::create(['body' => 'create']);
return History::get()->last()->log;
});
Route::get('/create-and-update', function () {
History::create([]);
Post::create(['body' => 'create']);
Post::get()->last()->update(['body' => 'create-and-update' . time()]);
return History::get()->last()->log;
});
Route::get('/get-and-update', function () {
History::create([]);
Post::get()->last()->update(['body' => 'get-and-update' . time()]);
return History::get()->last()->log;
});
Route::get('/update', function () {
$post = Post::get()->last();
History::create([]);
$post->update(['body' => 'updated-' . time()]);
return History::get()->last()->log;
});
Route::get('/get', function () {
History::create([]);
Post::find(1);
return History::get()->last()->log;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment