log transactional data

This commit is contained in:
Omair Saleh
2022-12-16 13:39:19 +08:00
parent 2c1431a813
commit 96723461e4
4 changed files with 42 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace App\Classes\General\Traits;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
trait LogData
{
public static function boot()
{
parent::boot();
static::updating(function($model)
{
$tableName = Str::singular($model->table).'_logs';
$relationshipColumn = Str::singular($model->table).'_id';
$originalData = $model->getRawOriginal();
$originalData[$relationshipColumn] = $originalData['id'];
unset($originalData['id']);
if (!Schema::hasTable($tableName)) {
DB::statement('CREATE TABLE '.$tableName.' LIKE '.$model->table);
DB::statement('ALTER TABLE '.$tableName.' ADD COLUMN `'.$relationshipColumn.'` BIGINT NOT NULL AFTER `id`');
}
DB::table($tableName)->insert($originalData);
});
}
}
+2
View File
@@ -4,6 +4,7 @@ namespace App\Models;
use App\Classes\General\Interfaces\Remarkable;
use App\Classes\General\Interfaces\Transportable;
use App\Classes\General\Traits\LogData;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\MorphMany;
@@ -14,6 +15,7 @@ class Container extends AbstractModel implements Transportable, Remarkable
{
use HasRelationships;
use SoftDeletes;
use LogData;
protected $table = 'containers';
+2
View File
@@ -7,6 +7,7 @@ use App\Classes\General\Interfaces\Transportable;
use App\Classes\General\Interfaces\Packable;
use App\Classes\General\Interfaces\Transactionable;
use App\Classes\General\Traits\LogData;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -23,6 +24,7 @@ class PackingList extends AbstractModel implements Transportable, Steppable, Pac
use HasTableAlias;
use HasRelationships;
use SoftDeletes;
use LogData;
protected $table = 'packing_lists';
+2
View File
@@ -5,6 +5,7 @@ namespace App\Models;
use App\Classes\General\Interfaces\Documentable;
use App\Classes\General\Interfaces\Remarkable;
use App\Classes\General\Interfaces\Transactionable;
use App\Classes\General\Traits\LogData;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use Carbon\Carbon;
@@ -18,6 +19,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
class Transaction extends AbstractModel implements Documentable, Transactionable, Remarkable
{
use SoftDeletes;
use LogData;
protected $table = 'transactions';