Adding to the above answers. You could do something like this.
Create a class in app/models called BaseModel.php extending Eloquent
class BaseModel extends Eloquent{
public static function boot()
{
parent::boot();
static::creating(function($model)
{
//change to Auth::user() if you are using the default auth provider
$user = Confide::user();
$model->created_by = $user->id;
$model->updated_by = $user->id;
});
static::updating(function($model)
{
//change to Auth::user() if you are using the default auth provider
$user = Confide::user();
$model->updated_by = $user->id;
});
}
}
Then in your individual model classes you need to extent the BaseModel instead of Eloquent
class Product extends BaseModel {
protected $table = 'product';
//Booting the base model to add created_by and updated_by to all tables
public static function boot()
{
parent::boot();
}
}
Now any time you save or update a model, the created_by and updated_by fields would be updated automatically.
Note: This would only work when save or update is done through Eloquent. For query builder, you could have a common method to fetch and append the created_by and update_by column updates.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…