The Eloquent ORM is quite nice, though I'm wondering if there is an easy way to setup MySQL transactions using innoDB in the same fashion as PDO, or if I would have to extend the ORM to make this possible?
You can do this:
DB::transaction(function() { // });
Everything inside the Closure executes within a transaction. If an exception occurs it will rollback automatically.
If you don't like anonymous functions:
try { DB::connection()->pdo->beginTransaction(); // database queries here DB::connection()->pdo->commit(); } catch (\PDOException $e) { // Woopsy DB::connection()->pdo->rollBack(); }
Update: For laravel 4, the pdo
object isn't public anymore so:
try { DB::beginTransaction(); // database queries here DB::commit(); } catch (\PDOException $e) { // Woopsy DB::rollBack(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With