Transaction with Eloquent Laravel 5 -
i using myisam mysql , want use transaction here code:
db::transaction(function () { $project = project::find($id); $project->users()->detach(); $project->delete(); });
this code execute succesfuly not sure transaction works... how can test it?
there 2 ways of doing this, neither particularly nice, because db:transaction doesn't report errors.
put try/catch block inside closure , set external variable in catch block if transaction fails.
do manual transaction, using db::begintransaction , rollback / commit, again exception handler, per example:
db::begintransaction(); try { $project = project::find($id); $project->users()->detach(); $project->delete(); db::commit(); $success = true; } catch (\exception $e) { $success = false; db::rollback(); } if ($success) { // transaction worked ... }
Comments
Post a Comment