2017 © Pedro Peláez
 

library repobuilder

Create Migration,Model and Repository from console

image

dinkara/repobuilder

Create Migration,Model and Repository from console

  • Wednesday, July 18, 2018
  • by dinkara
  • Repository
  • 1 Watchers
  • 0 Stars
  • 167 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 24 % Grown

The README.md

RepositoryBuilder

Repobuilder is addon for PHP Laravel framework that implements Repository pattern and adds an extra level for database communication in our project architecture. Instead of accessing models and writing queries in your Controller classes or Models, you should place them here. This way you will create single access point with controlled set of functions to your database. Library also has necessary Laravel commands for easy creation of complete set of classes and files (migrations, models and repositories)., (*1)

Main advantages are: * Code reusability * Code transparency * Global usage
* Scalable, (*2)

Requirements

  • Laravel ~5.5 or higher

Composer Install

``` bash composer require dinkara/repobuilder, (*3)


## Publish service Add the service provider in `config/app.php`: ```php Dinkara\RepoBuilder\RepositoryBuilderServiceProvider::class,

For publishing new services you need to execute the following line, (*4)

``` bash php artisan vendor:publish --provider="Dinkara\RepoBuilder\RepositoryBuilderServiceProvider", (*5)


Now you are ready to use new features. To check if everything is fine, execute following command ``` bash php artisan list

If you can see make:repo option everything is ready., (*6)

How to use command

Examples :, (*7)

``` bash php artisan make:repo User, (*8)


In order to use your repositories as standard Laravel services you need to add following lines to your app/Providers/AppServiceProvider.php. This will point interface to proper class so Laravel knows which class to load when that interface is initialized. ``` php public function register() { /* $repos represents array of all you created with this library */ $repos = array( 'User', ); foreach ($repos as $idx => $repo) { $this->app->bind("App\Repositories\\{$repo}\I{$repo}Repo", "App\Repositories\\{$repo}\\Eloquent{$repo}"); } }

This command will create new folder User under App\Repositories and make two new files inside of them. Interface IUserRepo and class EloquentUser., (*9)

``` bash php artisan make:repo User --migation, (*10)


This command will create same like first command but it will also create new migration with given name. ``` bash php artisan make:repo User --model

This command will create same like first command but it will also create new model with given name in App\Models., (*11)

``` bash php artisan make:repo User --all, (*12)


This command will create migration, model and necessary repository classes. ## How to use repositories Repositories created with this library can be used in Controllers, Services, Seeders or any other class in your Laravel application. Basic example: ``` php <?php namespace App\Http\Controllers; use App\Repositories\User\IUserRepo; class UserController extends Controller { protected $userRepo; public function __construct(IUserRepo $userRepo) { $this->userRepo = $userRepo; } public function show($id) { if($item = $this->repo->find($id)){ return response()->json($item->getModel()); } return response('Not Found', 404); } public function update(Request $request, $id) { $data = $request->all(); if($this->userRepo->find($id)->update($data)){ return response("Ok", 200); } return response("Failed", 500); } /* ... Your code ... */ }

Base interface with all available functions:, (*13)

``` php <?php, (*14)

namespace Dinkara\RepoBuilder\Repositories;, (*15)

interface IRepo {, (*16)

function model();

function getModel();

function firstOrNew($where);

function firstOrCreate($where);

function fill($fields);

function find($id);

function all();

function paginateAll($perPage = 20);

function create($fields);

function findAndUpdate($id, $fields);

function update($fields);

function save();

function delete();

function __call($name, $arguments);

}, (*17)

Function "\_\_call" is a PHP magic function. It can be used to query database by attribute name.

Example:
``` php
$this->userRepo->findByEmail($email);

This can only be used for "findBy" function and it is recommended to use it only in combination with attributes that are unique, because it returns only first record., (*18)

Adding custom function to your repositories

If you want to add custom or override existing functions in your repositories, this can be done easily by changing interface and class for specific repository., (*19)

Let's have a look at our User example:, (*20)

You should first add new functions to App\Repositories\User\IUserRepo.php interface like shown below. ``` php <?php, (*21)

namespace App\Repositories\User;, (*22)

use App\Repositories\IRepo; /** * Interface UserRepository * @package App\Repositories\User */ interface IUserRepo extends IRepo { /** * Function that creates new user * @param type $fields */ function register($fields);, (*23)

/*
    ...
    Your custom functions
    ...
*/

}, (*24)

Afterwards you should define that functions in App\Repositories\User\EloquentUser.php class.

``` php
<?php

namespace App\Repositories\User;

use App\Repositories\EloquentRepo;
use App\Models\User;

class EloquentUser extends EloquentRepo implements IUserRepo {


    /**
     * Configure the Model
     * */
    public function model() {
        return new User;
    }

    public function register($fields) {

        $fields["confirmation_code"] = str_random(30);

        $result = $this->create($fields)->attachRole("customer");

        return $this->finalize($result);
    }


    private function attachRole($role) {
        if (!$this->model) {
            return false;
        }

        $result = $this->model->roles()->attach($role);

        return $this->finalize($this->model);
    }   

}

Repository pattern in this library is meant to cache state of your model object. This allows us to call multiple functions on the same model object (like we did with create and attachRole in the code above). To achieve this you have to return finalize function and pass it your model., (*25)

Do note that not all functions are meant to save state (for example when you have complex queries that return collection of data), in that case you should clear state of your model and just return eloquent result., (*26)

All suggestions and advices are welcome! So please send us your feedback and we will try to improve this library

The Versions

18/07 2018

dev-master

9999999-dev https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

18/07 2018

v0.3.1

0.3.1.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

05/07 2018

v0.3.0

0.3.0.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

20/05 2018

v0.2.9

0.2.9.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

16/04 2018

v0.2.8

0.2.8.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

16/04 2018

v0.2.7

0.2.7.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

16/04 2018

v0.2.6

0.2.6.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

23/01 2018

v0.2.5

0.2.5.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

01/12 2017

v0.2.4

0.2.4.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

27/11 2017

v0.2.3

0.2.3.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

19/10 2017

v0.2.2

0.2.2.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara
by Dzale

laravel repository eloquent repository pattern laravel repository pattern

17/10 2017

v0.2.1

0.2.1.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara

laravel repository php command eloquent console query artisan db oop builder repository pattern repo repos

16/10 2017

v0.1.7

0.1.7.0 https://github.com/dinkara/repobuilder

Create Migration,Model and Repository from console

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar dinkara

dinkara repobuilder