zhetenov/repository

Easy way to create repository

dev-master 2020-01-06 07:04 UTC

This package is not auto-updated.

Last update: 2025-06-17 12:13:30 UTC


README

Issues Forks Stars License

This package gives you an easy way to create repository in laravel by using command.

Installation

In your terminal:

composer require zhetenov/repository

Using

By using this command you can easily create repository:

php artisan make:repository User

You will see that there is a folder named Repositories and in this folder has User folder and it has 2 files UserRepository and User interface:

$ tree -a app
app
├── Console
├── Exceptions
├── Http
├── Providers
├── Repositories
│   └── User
│       ├── UserInterface.php
│       └── UserRepository.php
└── User.php

Repository

To begin, we need to return path of model in getModelClass method. And you can start working with repository pattern. For example to get all users we created method getAll. To start we called method startConditions which return copy of our User model(QueryBuilder). After that we can write own queryBuilder.

<?php

namespace app\Repositories\User;

use App\User;
use Illuminate\Database\Eloquent\Collection;
use Zhetenov\Repository\BaseRepository;

class UserRepository extends BaseRepository implements UserInterface
{
    /**
     * Returns current model.
     *
     * @return string
     */
    protected function getModelClass(): string
    {
        return User::class;
    }

    /**
     * @return Collection
     */
    public function getAll(): Collection
    {
        return $this
            ->startConditions()
            ->all();
    }
}