| @@ -17,9 +17,6 @@ abstract class ModelListener | |||||
| { | { | ||||
| // 履歴作成処理 | // 履歴作成処理 | ||||
| $this->createHistory($model); | $this->createHistory($model); | ||||
| // 更新者作成者設定処理 | |||||
| $this->setHandlerIds(); | |||||
| } | } | ||||
| protected function createHistory(IModelFeature $model) | protected function createHistory(IModelFeature $model) | ||||
| @@ -48,12 +45,4 @@ abstract class ModelListener | |||||
| $changeMessage | $changeMessage | ||||
| )); | )); | ||||
| } | } | ||||
| protected function setHandlerIds() | |||||
| { | |||||
| if (Auth::check()) { | |||||
| $this->created_by = Auth::id(); | |||||
| $this->updated_by = Auth::id(); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| @@ -3,6 +3,7 @@ | |||||
| namespace App\Listeners\Model; | namespace App\Listeners\Model; | ||||
| use App\Events\Model\UpdatingEvent; | use App\Events\Model\UpdatingEvent; | ||||
| use Illuminate\Support\Facades\Auth; | |||||
| class UpdatingListener extends ModelListener | class UpdatingListener extends ModelListener | ||||
| { | { | ||||
| @@ -10,6 +11,15 @@ class UpdatingListener extends ModelListener | |||||
| public function handle(UpdatingEvent $event): void | 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); | $this->handleEvent($event->model); | ||||
| } | } | ||||
| } | } | ||||
| @@ -2,8 +2,10 @@ | |||||
| namespace App\Models; | namespace App\Models; | ||||
| use Exception; | |||||
| use Illuminate\Database\Eloquent\Concerns\HasUuids; | use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||||
| use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
| use Illuminate\Support\Facades\Auth; | |||||
| use Illuminate\Support\Str; | use Illuminate\Support\Str; | ||||
| abstract class AppModel extends BaseModel | abstract class AppModel extends BaseModel | ||||
| @@ -13,7 +15,20 @@ abstract class AppModel extends BaseModel | |||||
| public function getHistory(): ?HistoryModel | public function getHistory(): ?HistoryModel | ||||
| { | { | ||||
| $historyName = static::class . 'History'; | $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 | public function getChangeLogMessage($before, $after): ?string | ||||
| @@ -4,7 +4,14 @@ namespace App\Models\Feature; | |||||
| use App\Models\HistoryModel; | use App\Models\HistoryModel; | ||||
| use Illuminate\Database\Query\Builder; | 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 | interface IModelFeature | ||||
| { | { | ||||
| @@ -0,0 +1,47 @@ | |||||
| <?php | |||||
| use App\Util\MigrationHelper; | |||||
| use Illuminate\Database\Migrations\Migration; | |||||
| use Illuminate\Database\Schema\Blueprint; | |||||
| use Illuminate\Support\Facades\Schema; | |||||
| return new class extends Migration | |||||
| { | |||||
| /** | |||||
| * Run the migrations. | |||||
| * | |||||
| * @return void | |||||
| */ | |||||
| public function up() | |||||
| { | |||||
| $tables = Schema::getAllTables(); | |||||
| foreach ($tables as $table) { | |||||
| $tableName = $table->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() | |||||
| { | |||||
| } | |||||
| }; | |||||