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.

  1. put try/catch block inside closure , set external variable in catch block if transaction fails.

  2. 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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -