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

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -