Code Monkey home page Code Monkey logo

laravel-repository's Introduction

Laravel-Repository

MIT License StyleCI Latest Stable Version codecov Build Status

About

framework for building repository pattern in Laravel.

Requirements

requires Laravel 6.0

Usage

Installation

$ composer require shintarosakata/laravel-repository

Artisan Commands

$ artisan make:repository <name>
$ artisan make:interface <name>
$ artisan make:entity <name>
$ artisan make:repositoryProvider <name>

RepositoryProvider is for binding repository and interface

Quick start

1. make repository, interface, entity and provider

Entity name -> singular

Other -> plural

$ artisan make:entity Sample
entity created successfully.

$ artisan make:repository Samples
repository created successfully.

$ artisan make:interface Samples
repository created successfully.

$ artisan make:repositoryProvider RepositoryProvider
Provider created successfully.

.
└── app
    ├── Entities
    │   └── Sample.php
    │
    ├── Repositories
    │   ├── Samples.php
    │   └── SamplesInterface.php
    │
    └── Providers
        └── RepositoryProvider

2. Binding Interfaces To Implementations

<?php

namespace App\Providers;

use shintarosakata\LaravelRepository\RepositoryProvider\RepositoryProvider as upstreamRepositoryProvider;

class RepositoryProvider extends upstreamRepositoryProvider
{
    // ...
    
    protected $repositories = [
        'Samples', // Add <name> here
    ];

    // ...

Register provider

./config/app.php

<?php

return [
    
    // ...
    
    'providers' => [
        // ...

        App\Providers\RepositoryProvider::class, // register provider
    ],
    
    // ...

];

3. Defining Repositories

<?php

namespace App\Repositories\Samples;

use shintarosakata\LaravelRepository\Repository\Repository;
use Illuminate\Database\Eloquent\Collection;
use App\Entities\Sample;

class Samples extends Repository implements SamplesInterface
{
    protected $table = 'samples'; // Add DB Table name here
    
    public function fetchFirst(): Sample
    {
        return $this->builder->first();
    }
    
    public function fetchAll(): Collection
    {
        return $this->builder->get();
    }
}

4. Defining Interface

<?php

namespace App\Repositories\Samples;

use shintarosakata\LaravelRepository\Repository\RepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use App\Entities\Sample;

interface SamplesInterface extends RepositoryInterface
{
    // Add public functions here...
    
    public function fetchFirst(): Sample;

    public function fetchAll(): Collection;
}

5. Defining Entity

<?php

namespace App\Entities;

use shintarosakata\LaravelRepository\Entity\Entity as upstreamEntity;

class Sample extends upstreamEntity
{
    // Add behavior here...
}

6. use!

<?php

namespace App\Http\Controllers;

use App\Repositories\Samples\SamplesInterface;

class SampleController extends Controller
{
    private $samples_repository;

    // Dependency injection
    public function __construct(SamplesInterface $samples_repository) {
        $this->samples_repository = $samples_repository;
    }

    public function index()
    {
        $first_sample_entity = $this->samples_repository->fetchFirst();
        return view('sample.index', $first_sample_entity);
    }
}

Contributing

Thank you for considering contributing to the Laravel-Repository

please send an e-mail to Shintaro Sakata [email protected].

Security Vulnerabilities

If you discover a security vulnerability within Laravel-Repository, please send an e-mail to Shintaro Sakata [email protected]. All security vulnerabilities will be promptly addressed.

License

Laravel-Repository is open-source software licensed under the MIT license.

laravel-repository's People

Contributors

shintarosakata avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-repository's Issues

Command is redundant

$ artisan make:repository <name>
$ artisan make:interface <name>
$ artisan make:entity <name>

the command that includes everything

$ artisan make:all in command <name>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.