Unit of Work (Entity Manager) design pattern Laravel implementation.
michalwolinski/unit-of-work is a Laravel package for unit of work (entity manager) design pattern laravel implementation..
It currently has 4 GitHub stars and 0 downloads on Packagist.
Install it with composer require michalwolinski/unit-of-work.
Discover more Laravel packages by michalwolinski
or browse all Laravel packages to compare alternatives.
Last updated
Unit of Work (Entity Manager) design pattern Laravel implementation.
composer require michalwolinski/unit-of-work
in console to install this library.I propose to use Dependency Injection to inject UnitOfWorkInterface.
Example implementation in service class:
use App\User;
use MichalWolinski\UnitOfWork\Interfaces\UnitOfWorkInterface;
class Service {
/**
* @var UnitOfWorkInterface
*/
private UnitOfWorkInterface $unitOfWork;
public function __construct(UnitOfWorkInterface $unitOfWork)
{
$this->unitOfWork = $unitOfWork;
}
public function example()
{
$user = new User();
$user->email = '[email protected]';
$user->name = 'Michal Wolinski';
$user->password = 'secret';
$user2 = new User();
$user2->email = '[email protected]';
$user2->name = 'John Doe';
$user2->password = 'secret';
// CREATE RECORDS
$this->unitOfWork->insert($user);
$this->unitOfWork->insert($user2);
$this->unitOfWork->commit();
dump($user2->getKey());
// UPDATE RECORDS
$user2->name = 'Jane Doe';
$this->unitOfWork->update($user2);
$this->unitOfWork->commit();
// REMOVE RECORDS
$this->unitOfWork->delete($user2);
$this->unitOfWork->commit();
}
}
This project is licensed under the MIT License.