3
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 5 years have passed since last update.

Laravel5.7でbeginTransaction,commitメソッドの実行ログを出力する

Last updated at Posted at 2019-02-10

概要

Laravel5.7で、DBファザードのbeginTransactionメソッド、commitメソッドの実行ログを出力する方法。

DB::getQueryLog()を実行することで、QueryBuilderが生成したSQLを出力を取得することができますが、beginTransactioncommitメソッドの実行ログは出力されません。transactionの流れを確認をしたいことがあったので、下記の方法で確認しました。

詳細な説明は省いていますが、ご参考になれば幸いです。

方法

app/Providers/EventServiceProvider.phpboot()に下記処理を追加する。

app/Providers/EventServiceProvider.php
    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        Event::listen('Illuminate\Database\Events\TransactionBeginning', function () {
            \Log::debug('beginTransaction');
        });

        \DB::listen(function ($query) {
            \Log::debug($query->sql);
        });

        Event::listen('Illuminate\Database\Events\TransactionCommitted', function () {
            \Log::debug('commit');
        });

        Event::listen('Illuminate\Database\Events\TransactionRolledBack', function () {
            \Log::debug('rollBack');
        });
    }
3
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
3
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?