D
D
Dmitry Maslennikov2018-07-26 17:02:37
Exceptions
Dmitry Maslennikov, 2018-07-26 17:02:37

Why doesn't Laravel save data to DB on HttpException?

I'm trying to save data to the database, but if there is an HttpException in the code, then Laravel does not save anything. In this case, the code works fine without the last line throw new AppHttpException). Apparently there is a rollback, tk. id is incremented even if there is no entry in the table.
How to make saving and throwing an exception work together.

$order_wait = new OrderWait();
$order_wait->order_id = $order->id;
$order_wait->save();
throw new AppHttpException(null, 465071, 'user balance too low for inner contract', [$id, $user->id, $user->balance], 404);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anlamas, 2018-07-27
@anlamas

try {
    DB::beginTransaction();
    .....
    DB::commit();
} catch(Exception $exception) {
    DB::rollback();
    throw new AppHttpException(null, 465071, 'user balance too low for inner contract', [$id, $user->id, $user->balance], 404);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question