diff --git a/app/Listeners/Model/ModelListener.php b/app/Listeners/Model/ModelListener.php index 7164079..5abafba 100644 --- a/app/Listeners/Model/ModelListener.php +++ b/app/Listeners/Model/ModelListener.php @@ -17,9 +17,6 @@ abstract class ModelListener { // 履歴作成処理 $this->createHistory($model); - - // 更新者作成者設定処理 - $this->setHandlerIds(); } protected function createHistory(IModelFeature $model) @@ -48,12 +45,4 @@ abstract class ModelListener $changeMessage )); } - - protected function setHandlerIds() - { - if (Auth::check()) { - $this->created_by = Auth::id(); - $this->updated_by = Auth::id(); - } - } } diff --git a/app/Listeners/Model/UpdatingListener.php b/app/Listeners/Model/UpdatingListener.php index c5d87d8..2c2a7bc 100644 --- a/app/Listeners/Model/UpdatingListener.php +++ b/app/Listeners/Model/UpdatingListener.php @@ -3,6 +3,7 @@ namespace App\Listeners\Model; use App\Events\Model\UpdatingEvent; +use Illuminate\Support\Facades\Auth; class UpdatingListener extends ModelListener { @@ -10,6 +11,15 @@ class UpdatingListener extends ModelListener public function handle(UpdatingEvent $event): void { + // 更新者作成者の設定 + if (Auth::check()) { + $id = Auth::id(); + $event->model->updated_by = $id; + if ($event->model->created_by === null) { + $event->model->created_by = $id; + } + } + $this->handleEvent($event->model); } } diff --git a/app/Models/AppModel.php b/app/Models/AppModel.php index d40e4b6..b33f4f0 100644 --- a/app/Models/AppModel.php +++ b/app/Models/AppModel.php @@ -2,8 +2,10 @@ namespace App\Models; +use Exception; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Str; abstract class AppModel extends BaseModel @@ -13,7 +15,20 @@ abstract class AppModel extends BaseModel public function getHistory(): ?HistoryModel { $historyName = static::class . 'History'; - return new $historyName; + + + $history = new $historyName; + + if ($history instanceof HistoryModel) { + if (Auth::check()) { + $id = Auth::id(); + $history->updated_by = $id; + $history->created_by = $id; + } + return $history; + } else { + throw new Exception("履歴モデル不正"); + } } public function getChangeLogMessage($before, $after): ?string diff --git a/app/Models/Feature/IModelFeature.php b/app/Models/Feature/IModelFeature.php index 866bac6..f6f2e94 100644 --- a/app/Models/Feature/IModelFeature.php +++ b/app/Models/Feature/IModelFeature.php @@ -4,7 +4,14 @@ namespace App\Models\Feature; use App\Models\HistoryModel; use Illuminate\Database\Query\Builder; - +use Illuminate\Support\Carbon; + +/** + * @property ?Carbon $updated_at + * @property ?Carbon $created_at + * @property ?string $updated_by + * @property ?string $created_by + */ interface IModelFeature { diff --git a/database/migrations/2023_09_04_155700_createdby_updatedby_change_to_uuid.php b/database/migrations/2023_09_04_155700_createdby_updatedby_change_to_uuid.php new file mode 100644 index 0000000..66ffbed --- /dev/null +++ b/database/migrations/2023_09_04_155700_createdby_updatedby_change_to_uuid.php @@ -0,0 +1,47 @@ +tablename; + if (!Schema::hasColumn($tableName, "created_by") || !Schema::hasColumn($tableName, "updated_by")) continue; + + $type = Schema::getColumnType($tableName, "created_by"); + + if (!in_array($type, ['uuid', 'guid'])) { + Schema::table($tableName, function (Blueprint $table) { + $table->dropColumn("created_by"); + $table->dropColumn("updated_by"); + }); + Schema::table($tableName, function (Blueprint $table) { + $table->uuid("created_by")->nullable()->comment("作成者ID");; + $table->uuid("updated_by")->nullable()->comment("更新者ID");; + }); + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +};