1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel 8でObserverを使ってレコード操作ユーザー記録機能を実装する

Posted at

Observer作成

【前提】レコード操作ユーザーの記録対象とするModelのテーブルに、下記のカラムが定義されていること。

  • created_by
  • updated_by
  • deleted_by
  • restored_by
  • artisan make:observerで作成
php artisan make:observer AuditObserver

Observer created successfully.

app/Observers/AuditObserver.phpが作成される。

  • app/Observers/AuditObserver.php
<?php

namespace App\Observers;

// ①追加:ここから
use Illuminate\Database\Eloquent\Model;
// ①追加:ここまで

class AuditObserver
{
    // ②変更:BEFORE
    //
    // ②変更:AFTER
    /**
     * 当該モデルのcreated_byに操作を実行したユーザーのIDをセットする
     *
     * @param  Model  $model
     */
    public function creating(Model $model)
    {
        $model->created_by = auth()->user() ? auth()->user()->id : 0;
    }

    /**
     * 当該モデルのupdated_byに操作を実行したユーザーのIDをセットする
     *
     * @param  Model  $model
     */
    public function updating(Model $model)
    {
        $model->updated_by = auth()->user() ? auth()->user()->id : 0;
    }

    /**
     * 当該モデルのupdated_byに操作を実行したユーザーのIDをセットする
     *
     * @param  Model  $model
     */
    public function saving(Model $model)
    {
        $model->updated_by = auth()->user() ? auth()->user()->id : 0;
    }

    /**
     * 当該モデルのdeleted_byに操作を実行したユーザーのIDをセットする
     *
     * @param  Model  $model
     */
    public function deleting(Model $model)
    {
        $model->deleted_by = auth()->user() ? auth()->user()->id : 0;
    }

    /**
     * 当該モデルのrestored_byに操作を実行したユーザーのIDをセットする
     *
     * @param  Model  $model
     */
    public function restoring(Model $model)
    {
        $model->restored_by = auth()->user() ? auth()->user()->id : 0;
    }
    // ②変更:ここまで
}

trait作成

mkdir -p app/Traits
  • app/Traits/AuditObservable.php
<?php

namespace App\Traits;

use App\Models\User;
use App\Observers\AuditObserver;

trait AuditObservable
{
    public static function bootAuditObservable()
    {
        self::observe(AuditObserver::class);
    }

    /**
     * レコード作成ユーザーを取得して返す
     *
     * @return mixed
     */
    public function createdBy()
    {
        return $this->hasOne(User::class, 'id', 'created_by');
    }

    /**
     * レコード更新ユーザーを取得して返す
     *
     * @return mixed
     */
    public function updatedBy()
    {
        return $this->hasOne(User::class, 'id', 'updated_by');
    }

    /**
     * レコード削除ユーザーを取得して返す
     *
     * @return mixed
     */
    public function deletedBy()
    {
        return $this->hasOne(User::class, 'id', 'deleted_by');
    }

    /**
     * レコード復元ユーザーを取得して返す
     *
     * @return mixed
     */
    public function restoredBy()
    {
        return $this->hasOne(User::class, 'id', 'restored_by');
    }
}

ModelクラスでAuditObservableトレイトを読み込み

  • app/Models/Post.php
<?php

namespace App\Models;

// ①追加:ここから
use App\Traits\AuditObservable;
// ①追加:ここまで
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    // ②追加:ここから
    use AuditObservable;
    // ②追加:ここまで
    use HasFactory;
    use SoftDeletes;

    // ...
}

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?