LoginSignup
0
0

More than 3 years have passed since last update.

LaravelでEloquentやQueryBuilderをきちんと書いているのにDBにデータが挿入されていない問題について

Posted at

ご挨拶

皆さんこんにちは。tyamahoriです。普段はWEB制作会社でサーバーサイドエンジニアをしています。

俺の屍を越えてゆけ

というわけで、本日(記事執筆日は2020.5.21)の午前中に発覚したしょうもないミスをここに書き残します。みなさんのお役に立つために恥ずかしい部分を晒していきます。

LaravelのTransaction処理でDB::commit()は忘れるな。絶対にだ。

何を当たり前なことを言っている僕でしょうか。。はいそうです。僕です。この処理が漏れていてDBに入るべきデータが入っていないというだめな状況になってしまいました。原因を探るのに2時間位時間を消費してしまったため、猛反省しております。。

気をつけるところ。

例えばTransaction処理を使うところにおいて、以下のような処理があったとします。

misstake.php

        DB::beginTransaction();

        try {

            return $this->sampleRepository
                ->persistSomeData($param1, $param2)
                ->getOrm()
                ->toArray();
        } ...

これではだめですね。。レスポンスは期待したもので正しくても、DB::commitがないためDBにデータが挿入されません。

collect.php

        DB::beginTransaction();

        try {
            $arrayResponse = $this->sampleRepository
                ->persistSomeData($param1, $param2)
                ->getOrm()
                ->toArray();

            DB::commit();

            return $arrayResponse;
        } ...

こういうふうにちゃんとしなくちゃだめですね。。トホホ。。。。

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