Skip to content

FarhadShirmardi/laravel-repository-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Repository Pattern Generator

Generate repository structure with single command

This package generates repository pattern files related to a model to use them in your app.

Installation

Require this package with composer using the following command:

composer require derakht/repository-pattern

Configuration

Publish the configuration file with th following command.

php artisan vendor:publish --provider="Derakht\RepositoryPattern\RepositoryPatternServiceProvider"

You can change default paths in this file.

Usage

You can now run the below command to create repository files.

php artisan make:repository ModelName

Example

php artisan make:repository Post

PostController.php

<?php

namespace App\Http\Controllers;

use App\Repositories\Contract\PostRepositoryInterface;

class PostController extends Controller
{
    public $postRepository;

    public function __construct()
    {
        $this->postRepository = app(PostRepositoryInterface::class);
    }

    public function index()
    {
        return $this->postRepository->all();    
    }
}

or if you want to use injection add App\Providers\RepositoryServiceProvider::class to provider array in config/app.php.

PostController.php

<?php

namespace App\Http\Controllers;

use App\Repositories\Contract\PostRepositoryInterface;

class ReportController extends Controller
{
    public $postRepository;

    public function __construct(PostRepositoryInterface $postRepository)
    {
        $this->postRepository = $postRepository;
    }

    public function index()
    {
        return $this->postRepository->all();    
    }
}

About

Implement repository pattern design with a command

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages