Search code examples
phplaravellaravel-5.3laravel-5.4

for loop insert query turn into update query in transaction function


i check my query .It insert 1st loop after that insert query turn into update is there anyway how to fix this

$database = New database;
DB::transaction(function () use ($database) {
         for ($i=0; $i < 5; $i++) {
           database->save();
         }
       });

here is my transaction picture


Solution

  • Put $database = New database; inside the for statement. Because in each loop you need a new database object to do the insertion. Currently, you make an object, then the first loop does the insertion and the reset of loops will update the same object.