Created
September 3, 2017 05:38
-
-
Save tabacitu/093726a8b4172960c4c32ab96ee67def to your computer and use it in GitHub Desktop.
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