Laravel Eloquent Orm Transactions

Laravel Eloquent ORM Transactions

You can do this:

DB::transaction(function() {
//
});

Everything inside the Closure executes within a transaction. If an exception occurs it will rollback automatically.

How can i use Transaction with Eloquent Laravel 5.5

You can try this way,

        DB::beginTransaction();
try {
$project = Project::find($id);
$project->users()->detach();
$project->delete();
DB::commit();
} catch (\Exception $ex) {
DB::rollback();
return response()->json(['error' => $ex->getMessage()], 500);
}

Transaction across multiple Eloquent Models using Laravel 5.1

Turns out the issue has nothing to do with the code I provided in the question.

The problem was that my tables were being created using MyISAM which does not support transactions.

Using InnoDB instead makes the code work as expected. To do so add $table->engine = 'InnoDB'; to your migrations.

Laravel Sum transactions amount grouped by category

Move the criteria in the WHERE clause to the ON clause of the join, and use a left join:

$grouped = DB::table('categories c')
->leftJoin('transactions t', function ($join) {
$join->on('t.category_id', '=', 'c.id')
$join->on(DB::raw('MONTH(t.date) = ?'))
})
->groupBy('c.display_name')
->selectRaw('SUM(t.amount) AS sum, c.display_name AS name')
->setBindings([$currentMonth])
->pluck('sum', 'name');

The raw MySQL query I am suggesting here is:

SELECT
c.display_name,
SUM(t.amount) AS sum
FROM categories c
LEFT JOIN transactions t
ON c.id = t.category_id AND
MONTH(t.date) = ?
GROUP BY
c.display_name;

The structure used above in the left join is critical here, because it ensures that every category on the left side of the join would appear in the result set, even if it have no matching transactions for a given month.

Laravel transactions with logic

This is how it works. If inside DB::transaction exception is thrown then transaction is rolled back automatically. However in your implementation exception is not thrown because you catch it inside transaction and just try to return error response (what by the way won't work because you miss return in DB::transaction(function() use ($customer_id, $request) { line).

The easiest way to solve is to catch exception not inside DB::transaction but outside of it - then it will behave as you expected, transaction will be rolled back.

Alternative solution in some cases it not using DB::transaction but instead using manual:

DB::beginTransaction();
DB::rollBack();
DB::commit();

as described in documentation.

laravel db transaction context is available on called functions

after several test i can tell that no closure is needed for model too, even if you have a create method inside another function, and exception is throwed -in the main or in the function- no problem with the rollback and commit steatment!
Well much more of that i was hoping!!!!

This is the code I tested:

   DB::beginTransaction();
try {
$this->pippo();
} catch (\Exception $ex) {
DB::rollback();
}
DB::commit();

public function pippo(){
$type=Cga_type::create(['name'=>'vvvv','description'=>'yyy']);
throw new Exception('error');

}

If I comment the transactions functions the record is written on the db, no otherwise!!!



Related Topics



Leave a reply



Submit