dev-master
9999999-dev
The Development Requires
Wallogit.com
2017 © Pedro Peláez
A basic Eloquent Repository for Laravel 5.1., (*1)
composer require gridprinciples/repository from your project directory.Add the following to the providers array in config/app.php:, (*2)
GridPrinciples\Repository\RepositoryServiceProvider::class,
Make a Repositories folder somewhere in your application, such as app/Repositories., (*3)
Make a new Repository by extending GridPrinciples\Repository:, (*4)
<?php
namespace App\Repositories;
use GridPrinciples\EloquentRepository;
class FooRepository extends EloquentRepository {
protected static $model = \App\Foo::class;
}
(Recommended) Use your repository in your controller(s):, (*5)
<?php
namespace App\Http\Controllers;
use App\Repositories\FooRepository;
public function __construct(FooRepository $repository)
{
$this->repository = $repository;
}
public function somePage($id)
{
$model = $this->repository->get($id);
if(!$model) {
// Model not found.
return abort(404);
}
return view('my_view', [
'foo' => $model,
]);
}
Some basic CRUD functionality is included with the EloquentRepository:, (*6)
You can call save with an array of data in order to make a new model/record., (*7)
$newModel = $this->repository->save([
'title' => 'This is indicative of a title',
'description' => 'You might have a description field, perhaps.',
]);
It is recommended you populate your model's $fillable array in order to avoid
mass-assignment problems., (*8)
You can select one or many records by their keys (usually id) using get:, (*9)
$singleModel = $this->repository->get(1); $multipleModels = $this->repository->get([2, 3, 4]);
If you'd like to retrieve many models and paginate them, use the index method:, (*10)
$pageOfModels = $this->repository->index(10); // 10 records per page
You can update models in a very similar way as creating, also by using the
save method:, (*11)
$data = [
'status' => 'active',
];
$id = 1;
$this->repository->save($data, $id);
You can also pass an array of keys as the second argument to save in order to
update many records at once., (*12)
Deleting models can be accomplished easily using the delete method:, (*13)
$this->repository->delete($id);
You can also pass an array of keys to delete in order to delete many records
at once., (*14)
This is open-sourced software licensed under the MIT license., (*15)