zatoday / repository
laravel的包仓库,所有功能采用eloquent、懒加载和升级模型
v1.2.1
2017-11-26 05:24 UTC
Requires
- php: >=7.0
- illuminate/console: ^5.5
- illuminate/container: ^5.5
- illuminate/database: ^5.5
- illuminate/support: ^5.5
Requires (Dev)
- mockery/mockery: dev-master
- phpunit/phpunit: ~6.0
This package is not auto-updated.
Last update: 2024-09-29 03:10:21 UTC
README
ZAToday Repository 是一个用于 Laravel 5.5 的包,用于抽象数据库层。这使得应用程序更容易维护。
功能
- all($columns = array('*'))
- lists($value, $key = null)
- paginate($perPage = 1, $columns = array('*'));
- create(array $data)
- update(array $data, $id, $attribute = "id") // 如果你使用mongodb,则需要指定主键$attribute
- delete($id)
- find($id, $columns = array('*'))
- findBy($field, $value, $columns = array('*'))
- findAllBy($field, $value, $columns = array('*'))
- findWhere($where, $columns = array('*')) // 懒加载
- with($relations)
- 自定义
make:model
安装
从您的终端运行以下命令
composer require zatoday/repository
用法
创建新的Repository文件
php artisan make:repository [options] [--] <name>
示例
// Create Repository is User php artisan make:repository User // Or create Repository is User and create model User php artisan make:repository -m User
<?php namespace App\Repositories; use ZAToday\Repository\Repository; class User extends Repository { public function model() { return \App\User::class; } }
通过实现model()
方法,您告诉仓库您想使用的模型类。现在,创建App\User
模型
<?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { }
最后,在控制器中使用仓库
<?php namespace App\Http\Controllers; use App\Repositories\User; class UserController extends Controller { private $user; public function __construct(User $user) { $this->user = $user; } public function index() { return \Response::json($this->user->all()); } }
可用方法
以下方法可用
ZAToday\Repository\Contracts\RepositoryInterface
public function all($columns = array('*')) public function - [x] lists($value, $key = null) public function - [x] paginate($perPage = 1, $columns = array('*')); public function - [x] create(array $data) public function - [x] update(array $data, $id, $attribute = "id") // if you use mongodb then you'll need to specify primary key $attribute public function delete($id) public function find($id, $columns = array('*')) public function findBy($field, $value, $columns = array('*')) public function findAllBy($field, $value, $columns = array('*')) public function findWhere($where, $columns = array('*'))
ZAToday\Repository\Contracts\CriteriaInterface
public function apply($model, Repository $repository)
示例用法
在仓库中创建新电影
$this->film->create(Input::all());
更新现有电影
$this->film->update(Input::all(), $film_id);
删除电影
$this->film->delete($id);
通过film_id查找电影;
$this->film->find($id);
您还可以选择要获取哪些列
$this->film->find($id, ['title', 'description', 'release_date']);
通过单个列的筛选条件获取单行。
$this->film->findBy('title', $title);
或者您可以通过单个列的筛选条件获取所有行。
$this->film->findAllBy('author_id', $author_id);
通过多个字段获取所有结果
$this->film->findWhere([ 'author_id' => $author_id, ['year','>',$year] ]);
筛选条件
筛选条件是将特定的条件或条件集应用于仓库查询的简单方式。您的筛选条件类必须扩展抽象类 ZAToday\Repository\Criteria
。
创建新的Criteria文件
php artisan make:criteria
示例
// Create Criteria
php artisan make:criteria UserMaxId
// Or create Criteria with folder User
php artisan make:criteria User\MaxId
这里有一个简单的筛选条件
<?php namespace App\Repositories\Criteria; use ZAToday\Repository\Criteria; use ZAToday\Repository\Contracts\RepositoryInterface as Repository; class UserMaxId extends Criteria { /** * @param $model * @param RepositoryInterface $repository * @return mixed */ public function apply($model, Repository $repository) { $model = $model->where('id', '<', 5); return $model; } }
现在,在您的控制器类中调用pushCriteria方法
<?php namespace App\Http\Controllers; use App\Repositories\Criteria\UserMaxId; use App\Repositories\User; class UserController extends Controller { /** * @var User */ private $user; public function __construct(User $user) { $this->user = $user; } public function index() { $this->user->pushCriteria(new UserMaxId); return \Response::json($this->user->all()); } }
或者
<?php namespace App\Http\Controllers; use App\Repositories\Criteria\UserMaxId; use App\Repositories\User; class UserController extends Controller { /** * @var User */ private $user; public function __construct(User $user) { $this->user = $user; } public function index() { $criteria = new UserMaxId return \Response::json($this->user->getByCriteria($criteria)->all()); } }
致谢
本包大量受到此优秀包的启发,由@Bosnadev。