Skip to content

Instantly share code, notes, and snippets.

@vmosoti
Forked from Nks/Model.php
Created October 27, 2017 07:11
Show Gist options
  • Save vmosoti/2a88050dfd0c57e95abd7415060099dd to your computer and use it in GitHub Desktop.
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
<?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