Reputation: 31
I have simple Laravel upload, insert/update.
I have ajax that uploads csv file to my directory..
$.ajax({
url:'uploadExcel',
data: new FormData($("#uploadForm")[0]),
headers: {
'X-CSRF-TOKEN' : $('meta[name="_token"]').attr('content')
},
// dataType:'json',
async: true,
type:'post',
cache: false,
processData: false,
contentType: false,
success: function(){
//chunk data from CSV
},
error: function(data){
var errors = data.responseJSON;
console.log(errors);
},
complete:function(completeData){
console.log(completeData);
}
});
After success, I chunk the data from csv. It chunks successfully using this link and now I'm using DB::transaction(function() {});
in inserting to database. But now i want to use the DB:COMMIT
and DB:ROLLBACK
...
Any idea on how I can do this?
For DB:COMMIT
I have a button that commits the last transaction.
For DB:ROLLBACK
I have a button that rolls everything back in the last transaction.
note: I'm doing ajax for this and it does not work.
Update: I do the manual way.
I have a function for beginTransaction
DB::beginTransaction();
foreach($results as $row){
$item = new items;
$selectQuery = DB::table('items')
->where('title', '=', $row["1"])
->where('description', '=', $row["2"])
->get();
if (count($selectQuery) > 0) {
// update
DB::table('items')
->where('title', '=', $row["1"])
->where('description', '=', $row["2"])
->update([
"title" => $row["1"],
"description" => $row["2"]
]);
} else {
//insert
$item->title = $row["1"];
$item->description = $row["2"];
$item->save();
}
}
and my 2nd function in my controller is DB::commit();
and the 3rd function is for DB:rollback();
.
This method does not save any data from csv and rollback and commit are not working. Any ideas?
Upvotes: 0
Views: 8649
Reputation: 4435
Give this a try. Define a separate connection for this purpose in your config/database.php
lets say temporary_connection
.
Now, in your Controller, instead of using transaction on DB
(it uses default connection) use the newly created connection.
Your method containing beginTransaction
:
$connection = DB::connection('temporary_connection');
$connection->beginTransaction();
//rest of the code
Your method containing rollback
:
$connection = DB::connection('temporary_connection');
$connection->rollback();
//rest of the code
Your method containing commit
:
$connection = DB::connection('temporary_connection');
$connection->commit();
//rest of the code
Upvotes: 0
Reputation: 10179
Laravel: Database Transactions
If you're using transaction method on the DB facade and If an exception is thrown within the transaction Closure, the transaction will automatically be rolled back. If the Closure executes successfully, the transaction will automatically be committed. You don't need to worry about manually rolling back or committing while using the transaction method:
DB::transaction(function () {
//your queries
});
Manually Using Transactions
If you would like to begin a transaction manually and have complete control over rollbacks and commits, you may use the beginTransaction method on the DB facade:
DB::beginTransaction();
You can rollback the transaction via the rollBack method:
DB::rollBack();
Lastly, you can commit a transaction via the commit method:
DB::commit();
For more detail go to docs: https://laravel.com/docs/5.2/database#database-transactions
Upvotes: 2
Reputation: 14027
If you pass a Closure to DB::transaction()
, Laravel will auto call DB::commmit
if no the Closure executes successfully and DB::rollBack
if an exception is being thrown.
If you want to have control over it (the manual way), use DB::beginTransaction()
For example:
DB::beginTransaction();
try{
...
DB::commit();
}catch(\Exception $e){
// echo $e->getMessage();
DB::rollBack();
}
Upvotes: 1