Created
August 18, 2016 15:30
-
-
Save Nks/49edd2e797539679469a031dcc3a52b2 to your computer and use it in GitHub Desktop.
Laravel 5.2 user spy-fields (created_by, updated_by, deleted_by) support
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
<?php | |
namespace App\Models; | |
use Auth; | |
use Illuminate\Database\Eloquent\Model as BaseModel; | |
class Model extends BaseModel | |
{ | |
protected $userAttributes = []; | |
public static function boot() | |
{ | |
parent::boot(); | |
static::updating(function ($model) { | |
if (in_array('updated_by', $model->userAttributes)) { | |
$model->created_by = Auth::user()->id; | |
} | |
}); | |
static::deleting(function ($model) { | |
if (in_array('deleted_by', $model->userAttributes)) { | |
$model->deleted_by = Auth::user()->id; | |
} | |
}); | |
static::saving(function ($model) { | |
if (in_array('created_by', $model->userAttributes)) { | |
$model->created_by = Auth::user()->id; | |
} | |
if (in_array('updated_by', $model->userAttributes)) { | |
$model->updated_by = Auth::user()->id; | |
} | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment