2

I am working with Laravel version 7, I want to insert data into two tables, inside_order and inside_order_total. I used transaction to control that data should insert in both, but if there be a problem not insert it in any of them. but transaction does not work correctly. I searched and tried many ways, but my problem was not solved.

This is my code:-

try {
    DB::beginTransaction();
    $request->validate([
        'menu_id' => 'required',
        'order_amount' => 'required',
        'order_price' => 'required',
        'total' => 'required',
        'table_order' => 'required'
    ], [
        'menu_id.required' => 'افزودن مینوی غذایی الزامی است.',
        'order_amount.required' => 'تعداد سفارشات الزامی است.',
        'total.required' => 'مقدار کلی پول باید بیشتر از صفر باشد.',
        'table_order.required' => 'انتخاب میز الزامی است',
        'order_price.required' => 'هیچ مقدار پولی وارد نشده است.',
    ]);

    $total = new InsideOrderTotal();
    $data = $request->all();
    $user = User::all();
    $total->location_id = $data['table_order'];
    $total->total = $data['total'];
    $total->identity = \random_int(100000, 999999);
    Notification::send($user, new newOrderNotification('سفارش جدید دارید!'));
    $total->save();

    foreach ($request->input('menu_id') as $item => $value) {
        $order = new InsideOrder();
        $order->total_id = $total->order_id;
        $order->menu_id = $data['menu_id'][$item];
        $order->order_amount = $data['order_amount'][$item];
        $order->price = $data['order_price'][$item];
        $order->save();
    }
    DB::commit();
    $response = array(
        'status' => 'success',
        'msg' => 'موفقانه انجام شد!',
    );
    return response($response);
} catch (\PDOException $e) {
    DB::rollBack();
    return redirect()->back()->with('errors', 'error');
}

I have tried this:-

DB::transaction(function() {

});

And this:-

DB::connection('tools')->beginTransaction();
DB::connection('tools')->commit();
DB::connection('tools')->rollBack();

But none of them worked. If I change a column name in my database in one table, it inserts in one of them. actually, it should not be like that.

Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38
Zia Yamin
  • 942
  • 2
  • 10
  • 34
  • There is a error, you need to catch the error `return $e->getMessage();` catch this before this line `DB::rollBack();` – STA Jul 25 '21 at 05:20
  • as I said, If I change a ``column name`` it says that column not found, but also insert data in another table, it should not insert data. – Zia Yamin Jul 25 '21 at 05:25
  • in your example if the error is not `\PDOException` related, the `rollback` won't run, but with `DB::transaction(function() { /* */ });` should work... the engine you using support transactions? – Juan Eizmendi Jul 25 '21 at 05:33
  • `DB::commit();` wont work if it catch any kind of error, so it will rollback the tables – STA Jul 25 '21 at 05:34
  • How can I undrestand that engin support or not? @JuanEizmendi – Zia Yamin Jul 25 '21 at 05:34
  • `SHOW CREATE TABLE ;` from that you can get the name, then see the spec – Juan Eizmendi Jul 25 '21 at 05:40
  • I am so sorry to ask this, where should I use ``SHOW CREATE TABLE ;``? @JuanEizmendi – Zia Yamin Jul 25 '21 at 05:43
  • if you're using MySQL [check here](https://stackoverflow.com/questions/213543/how-can-i-check-mysql-engine-type-for-a-specific-table), you can use directly console or `DB::statement(sql_query)`, if you aren't using MySQL check the docs – Juan Eizmendi Jul 25 '21 at 05:50
  • When you say "if you change column name" you mean you are forcing an error to happen to test if the transaction is working correctly right? – apokryfos Jul 25 '21 at 06:42
  • @apokryfos yes I want to test transaction working or not, but not working. – Zia Yamin Jul 25 '21 at 06:56
  • @JuanEizmendi the engine of the table is ``MyISAM``. `` – Zia Yamin Jul 25 '21 at 07:02
  • [myisam storage engine](https://dev.mysql.com/doc/refman/8.0/en/myisam-storage-engine.html) check it out it DOESN'T support transactions – Juan Eizmendi Jul 25 '21 at 07:06
  • @JuanEizmendi what is the solution to use transaction here? – Zia Yamin Jul 25 '21 at 07:22
  • @ZiaYamin change the table storage engine to InnoDB there's a guide [here](https://dev.mysql.com/doc/refman/8.0/en/converting-tables-to-innodb.html). InnoDB is (in my opinion at least) much better than MyISAM anyway – apokryfos Jul 25 '21 at 07:31
  • 1
    [Changing MySQL engine](https://stackoverflow.com/questions/4240977/changing-table-engine-in-mysql) [Memento pattern](https://en.wikipedia.org/wiki/Memento_pattern) – Juan Eizmendi Jul 25 '21 at 07:31
  • @JuanEizmendi Thanks, I changed table engine to ``InnoDB`` and now it is working. – Zia Yamin Jul 26 '21 at 05:30

0 Answers0