gridprinciples/repository
最新稳定版本:0.1.1
Composer 安装命令:
composer require gridprinciples/repository
包简介
README 文档
README
A basic Eloquent Repository for Laravel 5.1.
Installation
-
Run
composer require gridprinciples/repositoryfrom your project directory. -
Add the following to the
providersarray inconfig/app.php:GridPrinciples\Repository\RepositoryServiceProvider::class,
-
Make a Repositories folder somewhere in your application, such as
app/Repositories.
Usage
-
Make a new Repository by extending GridPrinciples\Repository:
<?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):
<?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:
Creating
You can call save with an array of data in order to make a new model/record.
$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.
Reading
You can select one or many records by their keys (usually id) using get:
$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:
$pageOfModels = $this->repository->index(10); // 10 records per page
Updating
You can update models in a very similar way as creating, also by using the
save method:
$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.
Deleting
Deleting models can be accomplished easily using the delete method:
$this->repository->delete($id);
You can also pass an array of keys to delete in order to delete many records
at once.
License
This is open-sourced software licensed under the MIT license.
统计信息
- 总下载量: 155
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2015-09-20