-
-
Save vmosoti/2a88050dfd0c57e95abd7415060099dd 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 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