交易不在laravel工作

I have a problem with transactions in laravel. When I try to do this operation with the following code, the error appears on the screen as if I had no transaction.

\DB::transaction(function() use ($movement, $movementRows) {
   $movement->save();
   $movement->rows()->saveMany($movementRows);
});

and when I try to do it with the code below the first object is saved although the second one fails.

\DB::beginTransaction();
try {
     $movement->save();
     $movement->rows()->saveMany($movementRows);
      \DB::commit();
      $success = true;
} catch (\Exception $e) {
    $success = false;
    \DB::rollback();
}

if ($success) {
    // the transaction worked ...
}

Make sure you are using engine that supports transactions. InnoDB supports transactions but MyIsam doesn't. So open your database for example in PhpMyAdmin and make sure all tables are set to InnoDB and not MyISAM (assuming you are using MySQL - you haven't mentioned what database you are using).