guardian360/repository

Repository pattern used to abstract the database layer.

1.4.0 2022-05-13 11:32 UTC

This package is auto-updated.

Last update: 2024-04-13 15:34:08 UTC


README

Build Status Coverage Status

A base repository for Eloquent models.

Requirements

  • PHP >=7.0.0
  • PHP MongoDB driver (optional)

Installation

Install via composer.

$ composer require guardian360/repository

Usage

First, you should create your repository class. You can do this manually or by using Artisan by doing $ php artisan make:repository UserRepository. Your repository must extend \Guardian360\Repository\AbstractRepository and implement the model() method.

<?php

namespace App\Repositories;

use App\User;
use Guardian360\Repository\AbstractRepository as Repository;

class UserRepository extends Repository
{
    /**
     * Specify the model's class name.
     *
     * @return string
     */
    public function model()
    {
        return User::class;
    }
}

By implementing the model() method, you are telling the repository what model class you want to use. Next, in our case, it seems only logical to have a User model. Why don't we create one?

<?php

namespace App;

use Illuminate\Database\Eloquent\Model

class User extends Model
{
    //
}

Finally, you may use the repository in your controller, or anywhere else really.

<?php

namespace App\Http\Controllers;

use App\Repositories\UserRepository;

class UserController extends Controller
{
    /**
     * @var \App\Repositories\UserRepository
     */
    protected $users;

    /**
     * @return void
     */
    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    /**
     * Display a listing of the users.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return response()->json($this->users->all());
    }
}

Examples

Fetch a list of all users.

$this->users->all();

Find a user.

$this->users->find(1);

Find a user by an attribute.

$this->users->findBy('name', 'Joe');

Find all users by an attribute.

$this->users->findAllBy('role', 'admin');
$this->users->findAllBy('role', ['admin', 'guest']);

Create a new user.

$this->users->create($request->all());

Update an existing user.

$this->users->update($user, $request->all());
$this->users->update(1, $request->all());

Delete an existing user.

$this->users->delete($user);
$this->users->delete(1);
$this->users->delete([1, 2]);

Specifications

Specifications allow you to apply very specific conditions to the repository's query. You may use Artisan to generate a Specification class for you, using $ php artisan make:specification UserIsAdmin, or you may do so manually. Your specification must satisfy the \Guardian360\Repository\Contracts\SpecificationContract contract.

<?php

namespace App\Specifications;

use Guardian360\Repository\Contracts\SpecificationContract as Specification

class UserIsAdmin implements Specification
{
    /**
     * Apply the specification.
     *
     * @param  mixed  $query
     * @return mixed
     */
    public function apply($query)
    {
        return $query->where('role', 'admin');
    }
}

Then, you may push the Specification to the repository in your controller. You may use as many Specifications as you like.

<?php

namespace App\Http\Controllers;

use App\Specifications\UserIsAdmin;
use App\Repositories\UserRepository;

class UserController extends Controller
{
    /**
     * @var \App\Repositories\UserRepository
     */
    protected $users;

    /**
     * @return void
     */
    public function __construct(UserRepository $user)
    {
        $this->users = $users;
    }

    /**
     * Display a listing of the admins.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $admins = $this->users->pushSpec(new UserIsAdmin)->all();

        return response()->json($admins);
    }
}

Credits

This package is inspired by the following awesome packages:

Thanks guys, I could not have done this without you. :)