Skip to content

Instantly share code, notes, and snippets.

@Nks
Created August 18, 2016 15:30
Show Gist options
  • Save Nks/49edd2e797539679469a031dcc3a52b2 to your computer and use it in GitHub Desktop.
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
<?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