Created
September 3, 2017 05:38
Star
You must be signed in to star a gist
CreatedByTrait
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\Traits; | |
use Illuminate\Database\Eloquent\Model; | |
trait CreatedByTrait { | |
/** | |
* Stores the user id at each create & update. | |
*/ | |
public function save(array $options = []) | |
{ | |
if (\Auth::check()) | |
{ | |
if (!isset($this->created_by) || $this->created_by=='') { | |
$this->created_by = \Auth::user()->id; | |
} | |
$this->updated_by = \Auth::user()->id; | |
} | |
parent::save(); | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| RELATIONS | |
|-------------------------------------------------------------------------- | |
*/ | |
public function creator() | |
{ | |
return $this->belongsTo('App\User', 'created_by'); | |
} | |
public function updator() | |
{ | |
return $this->belongsTo('App\User', 'updated_by'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment