2024-04-05 05:22:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
|
|
trait CreatedUpdatedBy
|
|
|
|
{
|
|
|
|
public static function bootCreatedUpdatedBy()
|
|
|
|
{
|
|
|
|
// updating created_by and updated_by when model is created
|
|
|
|
static::creating(function ($model) {
|
|
|
|
if (!$model->isDirty('createdBy')) {
|
|
|
|
$model->createdBy = auth()->user() ? auth()->user()->id : null;
|
|
|
|
}
|
|
|
|
if (!$model->isDirty('updatedBy')) {
|
|
|
|
$model->updatedBy = auth()->user() ? auth()->user()->id : null;
|
|
|
|
}
|
|
|
|
if ($model->isDirty('createdOn') && !$model->isDirty('createdOn')) {
|
|
|
|
$model->createdOn = now();
|
|
|
|
}
|
2024-04-16 11:14:18 +00:00
|
|
|
// if (!$model->isDirty('status')) {
|
|
|
|
// $model->status = 11;
|
|
|
|
// }
|
2024-04-05 05:22:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// updating updated_by when model is updated
|
|
|
|
static::updating(function ($model) {
|
|
|
|
if (!$model->isDirty('updatedBy')) {
|
|
|
|
$model->updatedBy = auth()->user() ? auth()->user()->id : 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|