/laravel-repositories

An Eloquent repository package for Laravel 5

Primary LanguagePHPMIT LicenseMIT

Laravel Repositories

Laravel Repositories is a package for Laravel 5 that abstracts the database layer, making your app easier to maintain.

license

Installation

Run the following command:

composer require getwes/laravel-repositories

Add the RepositoryServiceProvider to your config.app file in the providers array.

WesMurray\Repositories\RepositoryServiceProvider::class

Once you have added the service provider to your config file, you can run php artisan vendor:publish to publish the repository.php config file.

Basic Usage

Let's create a user repository class, Note that any concrete repository class MUST extend WesMurray\Repositories\RepositoryAbstract class and implement a model() method.

<?php

namespace App\Repositories;

use App\User;
use WesMurray\Repositories\RepositoryAbstract;
use App\Repositories\Contracts\UserRepository as RepositoryInterface;

class UserRepository extends RepositoryAbstract implements RepositoryInterface
{
    public function model()
    {
        return User::class;
    }
}

Configuration

Let's update our repository.php configuration file with the repository interface and concrete repository implementation, so the RepositoryServiceProvider can bind them into the application.

config/repository.php

<?php

return [

    'repositories' => [
        App\Repositories\Contracts\UserRepository::class => App\Repositories\UserRepository::class
        
        //
    ]
];

This saves you time NOT having to create your own service provider to bind the repository services to your application.

And finally, use the repository in the controller:

<?php

namespace App\Http\Controllers;

use App\Http\Request;
use App\Http\Controllers\Controller;
Use App\Repositories\Contracts\UserRepository;

class UserController extends Controller
{
    protected $users;
    
    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }
    
    public function index()
    {
        return $this->users->get();
    }
}

Available Methods

The following methods are available:
WesMurray\Repositories\Traits\RepositoryAbstractMethodsTrait

public function get();
public function store(array $data);
public function update($id, array $data);
public function delete($id);
public function forceDelete($id); // If SoftDeletes() are enabled.
public function paginate($count);
public function findById($id);
public function findByLogin($username);
public function findBySlug($id);
Need more custom methods?
Contribute by creating a fork of this repository... Update and create a pull request for review.

Criteria

Criteria is a easy way to apply conditions to your query. Note your critiera class must extend the WesMurray\Repositories\Criteria\CriterionInterface.

Example

Let's get a listing of users that must be verified before they can be displayed in the application.

In your App\Http\Controllers\UserController.php, lets add the criteria.

<?php

namespace App\Http\Controllers;

use App\Http\Request;
use App\Http\Controllers\Controller;
Use App\Repositories\Contracts\UserRepository;

// Import Criteria
use App\Repositories\Criteria\UserMustBeVerified;

class UserController extends Controller
{
    protected $users;
    
    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }
    
    public function index()
    {
        return $this->users->withCriteria(new UserMustBeVerified())->get();
    }
}

Let's create the App\Repositories\Criteria\UserMustBeVerified.php.

<?php

namespace App\Repositories\Criteria;

use WesMurray\Repositories\Criteria\CriterionInterface;

class UserMustBeVerified implements CriterionInterface
{
    public function apply($model)
    {
        return $model->whereNotNull('verified_at');
    }
}

Eager Loading

Sometimes you may want to load relationships into your query. You can also apply eager loading along with additional criteria if you have any already defined.

Example

Let's extend our listing of users and also get all of their posts they have created.

App\Http\Controllers\UserController.php

<?php

namespace App\Http\Controllers;

use App\Http\Request;
use App\Http\Controllers\Controller;
Use App\Repositories\Contracts\UserRepository;

// Import Criteria
use App\Repositories\Criteria\UserMustBeVerified;

// Import EagerLoad
use WesMurray\Repositories\Eloquent\Criteria\EagerLoad;

class UserController extends Controller
{
    protected $users;
    
    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }
    
    public function index()
    {
        return $this->users->withCriteria([
            new UserMustBeVerified(), new EagerLoad('posts')
        ])->get();
    }
}

Code with <3 by getwes