Database transactions in laravel
Created at 19-Mar-2021 ,
By samar
Database transactions in laravel
Through the use of the programming language, we will work together to solve the "Database transactions in laravel" puzzle in this lesson.
-
--PATH app\Http\Controllers\<YourController.php>
use Illuminate\Support\Facades\DB; DB::transaction(function () { DB::table('employee')->where('id', 1)->delete(); DB::table('employeedetails')->where('EmpID', 1)->delete(); });
Database transactions method is useful in laravel when some queries executed successfully and anyone or someone of them fails for some reason or because of any error. If some action fails everything will be rolled back automatically. You don't need to worry about manually rolling back or committing while using the transaction method. Lets you have multiple queries to run which are related to each other like on deleting a record from employee table you have to delete a record from emoployeedetails table. If any error occurs in execution of the second statement, the first statement will be rolled back automatically. So that is the case how you use database transactions. -
--PATH app\Http\Controllers\<YourController.php>
$id = 2; DB::transaction(function () use ($id) { DB::table('employee')->where('id', $id)->delete(); DB::table('employeedetails')->where('EmpID', $id)->delete(); });
You can use the use() method to access variables which are outside of the DB::transaction() method. You have to pass every variables inside use() method which you want to use inside your queries. You can use multiple comma separated variable values in your use method. -
--PATH app\Http\Controllers\<YourController.php>
DB::beginTransaction(); try { DB::table('employee')->where('id', 1)->delete(); DB::table('employeedetails')->where('EmpID', 1)->delete(); DB::commit(); } catch (\Exception $e) { DB::rollback(); //throw $e; }
You can use the transaction method manually and you have complete control over DB::commit() and DB::rollback() methods which are used to execute the queries and rollback the executed queries. A transaction gives you the ability to safely perform a set of data-modifying SQL queries. If any one of the queries is not executed successfully then you can rollback all executed queries using DB::rollback() method.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Get comma separated email from input array
- Post table seeder laravel 10
- How to display user profile after login in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Call to a member function getRelationExistenceQuery() on array in Laravel
- Call to a member function pluck() on array
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Undefined property: stdClass::$title
- Use withCount() to Calculate Child Relationship Records
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- Automatically remove records using Prunable trait in Laravel
- How to create belongstomany relation using custom name on custom pivot table
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- Get domain name in laravel
- Add a subselect based on relationship using withAggregate method
- Laravel recursive function in controller
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Create project table with model and migration
- How to create new user without form submission in laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- How to get laravel errors folder in views directory in laravel
- How to prevent host header attack in Laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- How to upload local Laravel project to server ?
- Laravel insert query not working