
Database transactions in laravel
Answers 3
-
--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(); });
0Database 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(); });
0You 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; }
0You 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.
Random Code Snippet Queries: Laravel
- How to call Laravel route in jQuery
- Count all and get 10 records after where condition in laravel
- Add a subselect based on relationship using withAggregate method
- How to show data by ID in laravel?
- Laravel change date format
- Get Array of IDs from Eloquent Collection
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Class 'App\Providers\Auth' not found
- Permanently delete a record in laravel
- Ajax POST request in laravel
- Laravel form request validation
- How to get random string in Laravel
- How to get specific columns using Laravel eloquent methods
- Send post data from controller to view
- Add class to body in laravel view
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- Laravel 9 pagination with search filter
- Print last executed query in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- How to authenticate admin users in Laravel ?
- Use withCount() to Calculate Child Relationship Records
- Laravel hasmany select not working
- Get posts belongs to a specific user in Laravel
- 419 page expired error in Laravel
- How to check query string exists or not in laravel blade