Compare commits
No commits in common. "0ca5a445f5bd06b9a27f1ce26554b815a53d08f1" and "7cd5f1c4da9b261fa9cd96bd6e977bb876336872" have entirely different histories.
0ca5a445f5
...
7cd5f1c4da
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
trait HasHistory
|
||||
{
|
||||
public static function bootHasHistory()
|
||||
{
|
||||
static::observe(HistoryObserver::class);
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
enum HistoryAction: string
|
||||
{
|
||||
case Insert = 'insert';
|
||||
case Update = 'update';
|
||||
case Delete = 'delete';
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
use App\Models\History;
|
||||
|
||||
class HistoryObserver
|
||||
{
|
||||
public function created($model)
|
||||
{
|
||||
History::create([
|
||||
'model_id' => $model->id,
|
||||
'model_name' => $model->getTable(),
|
||||
'before' => null,
|
||||
'after' => $model->toArray(),
|
||||
'action' => HistoryAction::Insert,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updating($model)
|
||||
{
|
||||
History::create([
|
||||
'model_id' => $model->id,
|
||||
'model_name' => $model->getTable(),
|
||||
'before' => $model->getRawOriginal(),
|
||||
'after' => $model->getAttributes(),
|
||||
'action' => HistoryAction::Update,
|
||||
]);
|
||||
}
|
||||
|
||||
public function deleting($model)
|
||||
{
|
||||
History::create([
|
||||
'model_id' => $model->id,
|
||||
'model_name' => $model->getTable(),
|
||||
'before' => $model->getRawOriginal(),
|
||||
'after' => null,
|
||||
'action' => HistoryAction::Delete,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\HistoryAction;
|
||||
use App\UuidId;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class History extends Model
|
||||
{
|
||||
use HasFactory, UuidId;
|
||||
|
||||
protected $fillable = [
|
||||
'model_id',
|
||||
'model_name',
|
||||
'before',
|
||||
'after',
|
||||
'action',
|
||||
];
|
||||
|
||||
protected function casts()
|
||||
{
|
||||
return [
|
||||
'action' => HistoryAction::class,
|
||||
'before' => 'array',
|
||||
'after' => 'array',
|
||||
];
|
||||
}
|
||||
}
|
|
@ -3,15 +3,15 @@
|
|||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use App\HasHistory;
|
||||
use App\UuidId;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Infocyph\UID\UUID;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory, Notifiable, UuidId, HasHistory;
|
||||
use HasFactory, Notifiable, UuidId;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
|
@ -12,8 +11,6 @@ return new class extends Migration
|
|||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Builder::defaultMorphKeyType('uuid');
|
||||
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
$table->uuid('id')->primary();
|
||||
$table->string('last_name');
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('histories', function (Blueprint $table) {
|
||||
$table->uuid('id');
|
||||
$table->uuid('model_id');
|
||||
$table->string('model_name', 250);
|
||||
$table->jsonb('before');
|
||||
$table->jsonb('after');
|
||||
$table->enum('action', [ 'insert', 'update', 'delete' ]);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('histories');
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue