1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

レガシーシステムの仕様を継承しつつ、Laravel流に昇華させる抽象モデルクラスの実装

Last updated at Posted at 2025-12-10

はじめに

基幹システムをリプレイスする際、別のシステムが基幹システムと同じデータベースを参照・更新している箇所がある為、DB構造の変更に制約があった。

その制約の以下のようなタイムスタンプ系のカラムがありました。

  • create_user_code
  • update_user_code
  • create_datetime
  • update_datetime

レコードを更新するたびに値を入れる処理を書くのは面倒だ。

解決策 抽象モデルクラスの作成

こんな感じの抽象モデルクラスとモデルイベントのオブザーバーを作成した。
該当のモデルクラスに対して、この抽象モデルを継承させるだけでOK

使い方
// 各モデルはこれを継承するだけ
class Post extends AbstractAuditableModel
{
  // タイムスタンプのカラム名とか、作成者・更新者の処理とか
  // 全部親クラスで面倒見てくれる
}
抽象モデル
abstract class AbstractAuditableModel extends Model
{
    use AuditableObservable;
    
    public const CREATED_AT = 'create_datetime';
    public const UPDATED_AT = 'update_datetime';

    final public function createUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'create_user_code', 'user_code');
    }
    
    final public function updateUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'record_user_code', 'user_code');
    }
}
トレイト
trait AuditableObservable
{
    public static function bootAuditableObservable(): void
    {
        static::observe(AuditableObserver::class);
    }
}
オブザーバー
class AuthorObserver
{
    public function creating(AbstractAuthorModel $model): void
    {
        $model->create_user_code = auth()->user()->user_code
        $model->update_user_code = auth()->user()->user_code;
    }

    public function updating(AbstractAuthorModel $model): void
    {
        ...
    }

    public function saving(Model $model)
    {
        ...
    }
    
    public function deleting(Model $model)
    {
        ...
    }
}

まとめ

百テーブルぐらいが対象だったので、とても役に立った👍

が、ぶっちゃけ抽象クラスじゃなくてもTraitだけで良かった感も。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?