Add record audit action when poskod created/updated

This commit is contained in:
afiq.hamzah
2023-03-23 12:42:02 +08:00
parent 535515ca24
commit 50ab73701a
2 changed files with 58 additions and 10 deletions
+11 -1
View File
@@ -8,4 +8,14 @@ class Audit extends Model
{ {
protected $table = 'audit'; protected $table = 'audit';
protected $connection = 'mysql7'; protected $connection = 'mysql7';
} protected $fillable = [
'users',
'actions',
'norujukan',
'new_data',
'old_data',
'kodcaw',
'created_at',
'updated_at',
];
}
+47 -9
View File
@@ -4,6 +4,8 @@ namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Audit;
use Illuminate\Support\Carbon;
class Poskod extends Model class Poskod extends Model
{ {
@@ -16,25 +18,61 @@ class Poskod extends Model
* *
* @var array * @var array
*/ */
protected $fillable = [ protected $fillable = ['poskod', 'koddaerah', 'kodnegeri'];
'poskod',
'koddaerah', public static function boot()
'kodnegeri', {
]; parent::boot();
$current = Carbon::now();
$current = new Carbon();
static::created(function (Poskod $poskod) use($current){
Audit::create([
'users' => request()->all()['user']['name'],
'actions' => 'Tambah poskod',
'norujukan' => $poskod->recno,
'new_data' => $poskod->toJson(),
'old_data' => '',
'kodcaw' => request()->all()['user']['cawangan'],
'created_at' => $current,
'updated_at' => $current,
]);
});
static::updated(function (Poskod $poskod) use($current) {
Audit::create([
'users' => request()->all()['user']['name'],
'actions' => 'Kemaskini poskod',
'norujukan' => $poskod->recno,
'new_data' => $poskod->toJson(),
'old_data' => json_encode($poskod->getOriginal()),
'kodcaw' => request()->all()['user']['cawangan'],
'created_at' => $current,
'updated_at' => $current,
]);
});
}
/** /**
* The attributes excluded from the model's JSON form. * The attributes excluded from the model's JSON form.
* *
* @var array * @var array
*/ */
// protected $hidden = []; // protected $hidden = [],
public function negeri(): BelongsTo { public function negeri(): BelongsTo
{
return $this->belongsTo(Negeri::class, 'kodnegeri', 'kodnegeri'); return $this->belongsTo(Negeri::class, 'kodnegeri', 'kodnegeri');
} }
public function daerah(): BelongsTo
public function daerah(): BelongsTo { {
return $this->belongsTo(Daerah::class, 'koddaerah', 'koddaerah'); return $this->belongsTo(Daerah::class, 'koddaerah', 'koddaerah');
} }
} }