kyle-noland/laravel-base-repository

此包的最新版本(dev-master)没有可用的许可信息。

一个灵活的 Laravel 仓库模式基础,用于与 Eloquent ORM 一起工作

dev-master 2015-02-20 21:38 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:29:59 UTC


README

一个灵活的 Laravel 仓库模式基础,用于与 Eloquent ORM 一起工作。

此包的目标是在允许您即时创建基本查询的同时,为构建 Laravel Eloquent 仓库提供一个基础。

安装

通过 Composer 安装此包。编辑您的项目 composer.json 文件以添加对 kyle-noland/laravel-base-repository 的需求

"require": {
    "kyle-noland/laravel-base-repository": "dev-master"
}

通过终端更新 Composer

composer update

将服务提供者添加到您的 app/config/app.php 文件中

'KyleNoland\LaravelBaseRepository\LaravelBaseRepositoryServiceProvider'

用法

扩展 BaseRepository 类并实现您自己的自定义仓库逻辑

<?php namespace MyProject\Repositories;

use KyleNoland\LaravelBaseRepository\BaseRepository;
use MyProject\Interfaces\CompanyRepositoryInterface;
use MyProject\Models\Company;

class CompanyRepository extends BaseRepository implements CompanyRepositoryInterface {

	public function __construct(Company $model)
	{
		$this->model = $model;
	}
	
	// Add your repository methods here

}

BaseRepository 类提供基本的 COUNT、SELECT、INSERT、UPDATE、DELETE、WHERE、WHERE IN 子句,并能够懒加载相关模型。

统计所有模型

$count = $this->companyRepo->count();

统计模型子集

$count = $this->companyRepo->where('is_active', true)->count();

选择模型

$allCompanies = $this->companyRepo->all();
$activeCompanies = $this->companyRepo->where('is_active', true)->get();
$activeCopmaniesInTexas = $this->companyRepo->where('is_active', true)->where('state', 'TX')->get();