Skip to content

Instantly share code, notes, and snippets.

@tomysmile
Created January 11, 2016 07:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomysmile/62fc8bae4d89ddd9049c to your computer and use it in GitHub Desktop.
Save tomysmile/62fc8bae4d89ddd9049c to your computer and use it in GitHub Desktop.
Laravel: Adding Created_By
<?php

namespace App;

use Auth;
use Illuminate\Database\Eloquent\Model as Eloquent;

class BaseModel extends Eloquent{
  
     public static function boot()
     {
        parent::boot();
        static::creating(function($model)
        {
            $user = Auth::user();          
            $model->created_by = $user->id;
            $model->updated_by = $user->id;
        });
        static::updating(function($model)
        {
            $user = Auth::user();
            $model->updated_by = $user->id;
        });      
    }
  
}

and in your inherited/implementation page, for example: finance.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Finance extends BaseModel {
    
    public static function boot()
    {
        parent::boot();
    }
  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment