Recently in learning Laravel, in the study encountered a lot of problems, so think the summary record down, so the following this article mainly to you about laravel how to use database transactions and capture the transaction failure after the exception of the relevant information, the need for friends can refer to, the following to see together.
Objective
If you want to run a set of operations in a database transaction in Laravel, you can use the transaction method in db facade. If an exception is thrown within the closure of a transaction, the transaction is automatically restored. If the closure runs successfully, the transaction is automatically committed.
You don't have to worry about the need to manually restore or commit a transaction when using the transaction method:
Db::transaction (function () {db::table (' users ')->update ([' votes ' = 1]); Db::table (' posts ')->delete ();});
Manually Manipulating transactions
If you want to handle transactions manually and have full control over the restore or commit operation, you can use the BeginTransaction method in DB facade:
Db::begintransaction ();
You can also restore transactions by using the RollBack method:
Db::rollback ();
Finally, you can commit the transaction through the Commit method:
Db::commit ();
Note: The transaction method of the DB facade can also be used to control query-statement constructors and eloquent ORM transactions.
Example Introduction
If there is a knowledge point to store in the database, this knowledge point belongs to two different test centers, that is, the test center and the knowledge point of the two data is a many-to-many relationship, then to achieve this data structure requires three tables:
Knowledge Point Table Wiki:
---------------------------------------ID title content---------------------------------------
Test Center Table Tag:
-------------------ID Name-------------------
Knowledge Point Association Table Wiki_tag_rel
----------------------------------ID tag_id wiki_id----------------------------------
Now to open the transaction new wiki data, the new wiki is successful and then link it to the designated Test center up
(When using Query Builder in Laravel or eloquent ORM to execute query, if failure returns a Illuminate\database\queryexception exception)
<?phpnamespace app\http\controllers;use illuminate\http\request;use illuminate\database\queryexception;use App\ Wiki;class TestController extends controller{//transaction method with DB facade to control transaction public Function Storewiki of query Statement builder (Request $request) {db::begintransaction (); try {$tagIds = explode (', ', $request->get (' tag_id ')); $wiki _id = db::table (' wiki ')->insertgetid ([' title ' = + $request->get (' title '), ' content ' = $request-> ; Get (' content ')]); $relationData = []; foreach ($tagIds as $tagId) {$data = [' wiki_id ' = $wiki _id, ' tag_id ' + $tagId]; $relationData [] = $data; } db::table (' Wiki_tag_rel ')->insert ($relationData); Db::commit (); } catch (\illuminate\database\queryexception $ex) {db::rollback (); return \response::json ([' status ' = ' error ', ' error_msg ' = ' Failed ', please contact Supervisor ']); } return \response::json ([' status ' = ' OK ']); The transaction public function Createwiki (array $data) {db) of the eloquent ORM is controlled with the transaction method of the DB facade:: BeginTransaction (); try {$tagIds = explode (', ', $data [' tag_id ']); $newWiki = Wiki::create ([' title ' = ' + ' $data [' title '], ' content ' + $data [' content ']]; The wiki and tag two model use Belongstomany to establish a many-to-many relationship//attach method to attach the relationship between wiki and tag (write to Intermediate table) $newWiki->tags ()->attach ($ Tagids); Db::commit (); } catch (Queryexception $ex) {db::rollback (); return \response::json ([' status ' = ' error ', ' error_msg ' = ' Failed ', please contact Supervisor ']); } return \response::json ([' status ' = ' OK ']); }}
Related recommendations:
Detailed Laravel how to modify through the background. Env Configuration
Explore how the middleware of Laravel is implemented
Laravel Optimized Split routing file