aderemi / lara-repo
Laravel Repositories 是一个用于抽象数据库层的 Laravel 包。这使得应用程序的维护变得更加容易。
dev-master
2018-11-10 12:28 UTC
Requires
- php: >=7.0.0
- illuminate/console: ^5.2
- illuminate/database: ^5.2
- illuminate/filesystem: ^5.2
- illuminate/mail: ^5.2
- illuminate/support: ^5.2
Requires (Dev)
- codeception/codeception: ^2.3
This package is auto-updated.
Last update: 2024-09-11 02:38:39 UTC
README
Laravel Repositories 是一个用于抽象数据库层的 Laravel 包。这使得应用程序的维护变得更加容易。
安装
从您的终端运行以下命令
composer require "Aderemi/LaraRepo: @dev"
或者在您的 composer.json 文件中的 require 部分添加以下内容
"supermart_nigeria/library": "@dev"
然后运行 composer update
用法
首先,创建您的仓库类。请注意,您的仓库类必须扩展 LaraRepo\Repositories\Eloquent\Repository
并实现 model() 方法
<?php namespace App\Models\Repositories; use LaraRepo\Repositories\Contracts\RepositoryInterface; use LaraRepo\Repositories\Eloquent\Repository; class AdSlotRepository extends Repository { public function model() { return 'App\AdSlot'; } }
通过实现 model()
方法,您告诉仓库您想要使用哪个模型类。现在,创建 App\Models\AdSlot
模型
<?php namespace App; use Illuminate\Database\Eloquent\Model; class AdSlot extends Model { protected $primaryKey = 'id'; protected $table = 'ad_slots'; protected $slot = [ "head_section" => 1 ]; }
最后,在控制器中使用该仓库
<?php namespace App\Http\Controllers; use App\Models\Repositories\AdSlotRepository as AdSlot; class AdSlotsController extends Controller { private $adSlot; public function __construct(AdSlot $adSlot) { $this->adSlot = $adSlot; } public function index() { return \Response::json($this->adSlot->all()); } }
可用方法
以下方法可用
LaraRepo\Repositories\Contracts\RepositoryInterface
public function all($columns = array('*')) public function lists($value, $key = null) public function paginate($perPage = 1, $columns = array('*')); public function create(array $data) // if you use mongodb then you'll need to specify primary key $attribute public function update(array $data, $id, $attribute = "id") 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('*'))
LaraRepo\Repositories\Contracts\CriteriaInterface
public function apply($model, Repository $repository)
示例用法
在仓库中创建一个新的广告位
$this->adSlot->create(Input::all());
更新现有的广告位
$this->adSlot->update(Input::all(), $adSlot_id);
删除广告位
$this->adSlot->delete($id);
通过 adSlot_id 查找广告位;
$this->adSlot->find($id);
您还可以选择要获取哪些列
$this->adSlot->find($id, ['name', 'adLocation', 'created_by']);
通过单个列条件获取单行。
$this->adSlot->findBy('name', $name);
或者您可以通过单个列条件获取所有行。
$this->adSlot->findAllBy('adLocation', $adLocation);
通过多个字段获取所有结果
$this->adSlot->findWhere([ 'adLocation' => $adLocation, ['created_by','>',$created] ]);
条件
条件是应用特定条件或条件集到仓库查询的简单方法。您的条件类必须扩展抽象类 LaraRepo\Repositories\Criteria\Criteria
。
这是一个简单的条件
<?php namespace App\Models\Repositories\Criteria; use LaraRepo\Repositories\Criteria\Criteria; use LaraRepo\Repositories\Contracts\RepositoryInterface as Repository; class CreatedBefore extends Criteria { /** * @param $date * @return null */ public function __construct($date) { $this->fields[0] = $date; } protected function getSearchableFieldsConfig(): array { return [ $this->fields[0] => [ 'type' => self::FIELD_DATE, 'searchType' => self::DATE_SEARCH_BEFORE ] ]; } }
条件和过滤的区别是:条件可以用于任何模型,而过滤则绑定到模型
这是一个过滤条件
<?php namespace App\Models\Repositories\Criteria; use LaraRepo\Repositories\Criteria\Filter; class AdSlotOnlyOnPage extends Filter { protected $basicFields = ['id', 'adLocation', 'name']; // Fields to select protected function getSearchableFieldsConfig(): array { return [ "adLocation" => [ 'searchType' => self::STR_SEARCH_EQUALS ], "createdBy" => [ 'searchType' => self::DATE_SEARCH_BETWEEN, 'orWhere' => true ], "ads.publishedBy" => [ 'searchType' => self::DATE_SEARCH_BETWEEN, 'orWhere' => true ], "ads@name" => [ 'searchType' => self::STR_SEARCH_CONTAINS ] ]; } }
现在,在您的控制器类中调用 pushCriteria 方法
<?php namespace App\Http\Controllers; use App\Models\Repositories\Filters\AdSlotOnlyOnPage; use App\Repositories\AdSlotsRepository as AdSlot; class AdSlotsController extends Controller { /** * @var AdSlot */ private $adSlot; public function __construct(AdSlot $adSlot) { $this->adSlot = $adSlot; } public function index() { $on_page = new AdSlotOnlyOnPage(); //$on_page->fill(["adLocation" => 'landingPage']); $this->adSlot->pushFilter($on_page); //ONE LINER: $this->adSlot->pushFilter((new AdSlotOnlyOnPage())->fill(["adLocation" => 'landingPage'])) return \Response::json($this->adSlot->all()); } public function getAdSlotCreatedAt(Request $request) { $created_before = new CreatedBefore("created_at"); $created_before->fill(["created_at" => $request->created_at]); $this->adSlot->pushCriteria($created_before); //ONE LINER: return \Response::json($this->adSlot->pushCriteria((new CreatedBefore("created_at"))->fill($request->all()))); return \Response::json($this->adSlot->all()); } public function getAdSlotWithRelation() //This is eager-loaded { return $this->sendSuccess(["adSlots" => $this->adSlot->pushFilter(new AdSlotFilter())->with(['ads'])->all()]); } public function createAdSlot(Request $request) { $this->adSlot->validator['post'] = [ 'name' => 'required|unique:ad_slots|max:50', 'adLocation' => 'required', ]; $added_adSlot = $this->adSlot->create(); return \Response::json($added_adSlot); } public function updateAdSlot(Request $request) { $this->adSlot->validator['put'] = [ 'id' => 'required', 'name' => 'required|unique:ad_slots|max:50', 'adLocation' => 'required', ]; $updated_adSlot = $this->adSlot->update(); return \Response::json($updated_adSlot); } }
仍在编辑....[仍在工作中:这只是意图]