LaravelPackages.net
Acme Inc.
Toggle sidebar
antoiner/laravel-repositories-command

Command for Laravel to generate repositories of each model

1
0
About antoiner/laravel-repositories-command

antoiner/laravel-repositories-command is a Laravel package for command for laravel to generate repositories of each model. It currently has 0 GitHub stars and 1 downloads on Packagist. Install it with composer require antoiner/laravel-repositories-command. Discover more Laravel packages by antoiner or browse all Laravel packages to compare alternatives.

Last updated

Laravel Repositories Command

Artisan command to create Repository with RepositoryInterface for each Model and use it in the application with the dependency injection.

Installation

Require package with composer :

composer require antoiner/laravel-repositories-command --dev

Command

Once installed run the following command to create RepositoryInterface and Repository for each Model of your application :

php artisan make:repositories

This command also create RepositoryServiceProvider to bind all RepositoryInterface with its correspondent Repository .

After executing the command you just have to add this line in the config/app.php to register the service provider :

App\Providers\RepositoryServiceProvider::class,

Usage

Now you can use Repository in your application like this :

//routes/web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', "HomeController@index");

//app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use App\Repositories\UserRepositoryInterface;

class HomeController extends Controller
{
    private $userRepository;

    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function index() {
        dd($this->userRepository->all());
    }
}

Annotations

You can disable the repository generation for a Model by adding the annotation @Repository(enable = false) in the class documentation.

//Blog.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * @Repository(enable = false)
 */
class Blog extends Model
{
    //
}

Comments