Assuming you have already installed composer require jenssegers/mongodb
and configured it fully according to the documentation (https://packagist.org/packages/jenssegers/mongodb), now run this to install mongo-transactions:
composer require hema/mongo_transaction
use Hema\MongoTransaction\Transactions\Eloquent\Model
use use Jenssegers\Mongodb\Eloquent\Model
-
this package created for specific business needs. so, it's may not fit your needs 100%. but you still can fork it and extend or modify it.
-
you've to enable replica set in Mongo.
~$ sudo mongod --replSet "rs0"
and then run mongo
~$ sudo mongo
use Hema\MongoTransaction\Transactions\Transaction
* Transaction::start();
// ........ A bunch of other db operations
* Transaction::commit();
// ........ commit when success
* Transaction::rollback();
// ........ Or rollback when error occure
* create();
* insert();
* update();
* delete();
* forceDelete();
* restore()
* increment();
* decrement();
* trash(); //custome method for soft-delete
* remove(); //custome method for hard-delete
Transaction::start();
try {
User::insert($some_data);
Profile::insert($some_data);
Transaction::commit();
return 'done';
} catch (\Exception $e) {
Transaction::rollback();
return $e->getMessage();
}