php - Transaction in laravel 5.1 -


does transaction in laravel works db facade?. want know if works eloquent model; i.e. following code have intended effect

db::begintransaction();  try {     eloquentmodel::query(); } catch (\exception $e) {     db::rollback(); } 

yes, works both db facade , eloquent models.

(as example, connection used default, you're ok.)

don't forget db::commit(); when it's ok commit!

also, database table engine must support transactions, such innodb.

suggestion, can use simpler, transaction method

db::transaction(function () {      eloquentmodel::query();  }); 

also, don't afraid test out, increase confidence level. say, provoque error, see work

db::begintransaction();  try {     eloquentmodel::query();     eloquentmodel::create(['field_not_exists' => 'will throw exception!']); } catch (\exception $e) {     // db::rollback(); // test comment, , without comment, check db results ;) } db::commit(); 

happy coding!


Comments

Popular posts from this blog

How to check data type in C++? -

javascript - Is getTimezoneOffset() stable during daylight saving transition? -

formula - R: how to pass in a reference to the variable in glm or lm? -