caffeinated / repository
Reusable Laravel repository interface.
Installs: 4 620
Dependents: 1
Suggesters: 0
Security: 0
Stars: 29
Watchers: 3
Forks: 10
Open Issues: 0
Requires
- php: >=7.1.3
- illuminate/config: 5.4.*|5.5.*|5.6.*|5.7.*
- illuminate/support: 5.4.*|5.5.*|5.6.*|5.7.*
README
Caffeinated Repository
Getting Started
Introduction
The Caffeinated Repository package allows the means to implement a standard boilerplate repository interface. This covers the standard Eloquent methods in a non-static, non-facade driven way right out of the box. Fear not though Batman! The Caffeinated Repository package does not limit you in any way when it comes to customizing (e.g overriding) the provided interface or adding your own methods.
Installing Caffeinated Repository
It is recommended that you install the package using Composer.
composer require caffeinated/repository
This package is compliant with PSR-1, PSR-2, and PSR-4. If you find any compliance oversights, please send a patch via pull request.
Using Repositories
Create a Model
Create your model like you normally would. We'll be wrapping our repository around our model to access and query the database for the information we need.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model class Book extends Model { // }
Create a Repository
Create a new Repository class - usually these classes are simply stored within a Repositories
directory. There are a few requirements for each repository instance:
- Repository classes must extend the Caffeinated EloquentRepository class.
- Repository classes must specify a public property pointing to the model.
- Repository classes must specify an array of cache tags. These tags are used by the package to handle automatic cache busting when relevent values change within the database.
<?php namespace App\Repositories; use App\Models\Book; use Caffeinated\Repository\Repositories\EloquentRepository; class BookRepository extends EloquentRepository { /** * @var Model */ public $model = Book::class; /** * @var array */ public $tag = ['book']; }
Injecting a Repository
Once you've built and configured your repository instance, you may inject the class within your classes where needed:
<?php namespace App\Http\Controllers; use App\Repositories\BookRepository; class BookController extends Controller { /** * @var BookRepository */ protected $book; /** * Create a new BookController instance. * * @param BookRepository $book */ public function __construct(BookRepository $book) { $this->book = $book; } /** * Display a listing of all books. * * @return Response */ public function index() { $books = $this->book->findAll(); return view('books.index', compact('books')); } }