LaravelPackages.net
Acme Inc.
Toggle sidebar
michalwolinski/unit-of-work

Unit of Work (Entity Manager) design pattern Laravel implementation.

0
4
About michalwolinski/unit-of-work

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 for Laravel

Unit of Work (Entity Manager) design pattern Laravel implementation.


Installation by Composer

  1. Run
    composer require michalwolinski/unit-of-work
    
    in console to install this library.

Usage

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();
    }
}

Authors

License

This project is licensed under the MIT License.

Star History Chart