PHPackages                             g4t/laravel-ddd - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. g4t/laravel-ddd

ActiveLibrary[Framework](/categories/framework)

g4t/laravel-ddd
===============

Generates domains for laravel Domain Driven Development

35PHP

Since May 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/hussein4alaa/laravel-domain-driven-design)[ Packagist](https://packagist.org/packages/g4t/laravel-ddd)[ RSS](/packages/g4t-laravel-ddd/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel DDD
===========

[](#laravel-ddd)

### Domain Driven Development domains generator.

[](#domain-driven-development-domains-generator)

[![image](https://camo.githubusercontent.com/d46287e405dcb0b910aa1704d22f31cc47b3379ffdb1ca315ad2e6ee296fe50a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6734742f6c61726176656c2d6464642e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/d46287e405dcb0b910aa1704d22f31cc47b3379ffdb1ca315ad2e6ee296fe50a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6734742f6c61726176656c2d6464642e7376673f7374796c653d666c6174)[![image](https://camo.githubusercontent.com/820c22e069a675db750f50e391ca192dc14ef623bc50a8a9e08e4186121c2dc5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6734742f6c61726176656c2d6464642e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/820c22e069a675db750f50e391ca192dc14ef623bc50a8a9e08e4186121c2dc5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6734742f6c61726176656c2d6464642e7376673f7374796c653d666c6174)[![Build Status](https://camo.githubusercontent.com/2d66a3136401dd7219f202e6010e90f9e47a501efedc98a3eef718e633c998e9/68747470733a2f2f7472617669732d63692e6f72672f6734742f6c61726176656c2d6464642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/g4t/laravel-ddd.svg?branch=master)

`* Note: I did not create this library, i'm just updated it to work on the new releases of Laravel`

`* The owner of the library is: https://github.com/oleglfed/laravel-ddd`

This package is made to generate Domains, based on DB table. Package get all table fields and creates domain. With Domain creates repository, service and Infrastructure. Also the Package automatically binds generated classes to your app, so you can easily use DI or make Service by contract `$service = app(UserServiceInterface::class);`

`php artisan make:domain User --table=users`

Installation:
-------------

[](#installation)

Require this package with composer using the following command:

```
$ composer require g4t/laravel-ddd
```

Go to your `config/app.php` and add the service provider:

`\g4t\LaravelDDD\LaravelDddServiceProvider::class`

Usage
-----

[](#usage)

This package creates `app/Domain` and `app/Infrastructure` directories. So at first package should be able to create these two directories. Afterwards, you can revoke writable access from `app` directory.

This package requires writable permissions to config/domains directory. Before use, create `config/domains` directory with writable permissions or allow the package to write into `config` directory. It is necessary for writing domains binding. Afterwards, these domains will be bound to your app by LaravelDddServiceProvider

```
$ php artisan make:domain User --table=users
```

To generate domain, use the `make:domain` artisan command. This command will create in ths Domains and Infrastructures directories inside app/ folder domain classes.

E.g. for User will be created classes

`UserEloquent`

`UserRepository`

`EloquentUserRepository`

`UserService`

And contracts for this classes

### Available command options:

[](#available-command-options)

OptionDescription`table`Based on Table will be created Eloquent, getters and setters`directory`By default directory name takes from domain name. To override it --directory might be set`domain-path`By default domain directory is app/Domains. To override it --domain-path might be set`infrastructure-path`By default infrastructure directory is app/Infrastructures. To override it --infrastructure-path might be set### Advanced usage

[](#advanced-usage)

This package contains a few useful methods to work with services and repositories.

```
    public function index(UserServiceInterface $service)
    {
        $service->all(); //Shows all records

        $service->get(1); //Shows record with ID: 1

        $servcie->findWhere(['first_name' => 'Oleg']); //Returns all records with provided where

        $servcie->deleteWhere(['first_name' => 'Oleg']); //Deletes all records with provided where

        $servcie->firstOrCreate(['email' => 'oleg.fedoliak@gmail.com']); //Returns a record with provided where, or creates it

        $servcie->firstOrNull(['email' => 'oleg.fedoliak@gmail.com']); //Returns a record with provided where, or null

        $servcie->lists(20, 10, ['first_name', 'email']); //Returns paginated list with `per page`, `offset`, `columns` (all by default)

        $servcie->count(); //Returns count of records

        //Create
        $user = $service->newInstance();
        $user->setFirstName('Oleg');
        $user->setEmail('oleg.fedoliak@gmail.com');

        $user = $servcie->create($user);

        //Update
        $user->setFirstName('John');
        $user = $servcie->update($user);

        //Save. There is a method which will check if record is exists, and if so will update it. Otherwise create it.
        $user = $servcie->save($user);

        //Delete
        $isDeleted = $servcie->delete($user);
    }

```

If your routes use `resource` you can explicitly bind Domains Open `Providers\RouteServiceProvider` and add:

```
    \Route::model('user', App\Domains\User\UserEloquent::class);

```

Then you are able to use:

```
 public function update(Request $request, UserInterface $user, UserServiceInterface $service)
 {
    dd($user);
