kylenoland / eloquent-foundation
一套用于处理 Laravel Eloquent 仓库、模型、集合和观察者的基础类
1.0.3
2015-05-12 16:26 UTC
Requires
- php: >=5.4.0
- illuminate/support: >=4.2.0
This package is not auto-updated.
Last update: 2024-09-28 17:43:42 UTC
README
Eloquent Foundation 的目的是为 Laravel 的 Eloquent ORM 提供一组基础类,这将加速大多数项目的开发。你可以自由地使用你喜欢的 Eloquent Foundation 类的多少。
安装
通过 Composer 安装
composer require kyle-noland/eloquent-foundation
将服务提供者添加到 config/app.php
'providers' => [
...
'KyleNoland\EloquentFoundation\Providers\EloquentFoundationServiceProvider',
]
使用
Eloquent Foundation 是有偏见的,并对你的数据库字段名称做了一些假设。例如,PersonTrait 假设你的 Eloquent 模型至少包含以下属性
- first_name
- last_name
大多数 Eloquent Foundation 类都很小。你应该阅读它们以了解它们是如何工作的。
可能是 Eloquent Foundation 最通用的组件是 BaseRepositoryContract 和 BaseRepository 类。目的是提供一个 Eloquent 仓库实现,可以在此基础上构建,同时保持一些更常见的流畅 Eloquent 方法。 你可以免费获得大量的仓库样板代码,只需要在子类中编写你特定的应用方法。
例如,如果你在自己的 InvoiceRepository 类中扩展了 BaseRepository 类,你将获得大量的良好 Eloquent 方法 "免费",同时保持对 Eloquent 模型的直接访问在你的控制器或服务类之外。你可以做类似的事情
class SomeController extends Controller {
protected $invoiceRepo;
public function __construct(InvoiceRepositoryContract $invoiceRepo)
{
$this->invoiceRepo = $invoiceRepo;
}
public function something()
{
// Update a specific Invoice record
$invoice = $this->invoiceRepo->updateById($id, $data);
// Retrieve a specific Invoice record
$invoice = $this->invoiceRepo->getById($id);
// Retrieve all the Invoice records
$invoices = $this->invoiceRepo->all();
// Retrieve a Collection of Invoice records
$invoices = $this->invoiceRepo->whereIn('id', [1, 2, 3, 10])->get();
// Add the with() method to any "get-style" call to eager load related models
$invoices = $this->invoiceRepo->with('Company', 'Company.Users')->whereIn('id', [1, 2, 99])->get();
// Delete a particular Invoice record
$this->invoiceRepo->deleteById($id);
}
}
BaseRepository 类公开了以下样板方法,你可以在任何子类中利用这些方法
/**
* Get all the models
*
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function all();
/**
* Count the specified models
*
* @return int
*/
public function count();
/**
* Count the specified models
*
* @param $column
* @param $value
*
* @return int
*/
public function countBy($column, $value);
/**
* Create a new model
*
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $data);
/**
* Create one or more new model records in the database
*
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function createMultiple(array $data);
/**
* Delete models
*
* @return mixed
*/
public function delete();
/**
* Delete the specified model
*
* @param int $id
*
* @return int
*/
public function deleteById($id);
/**
* Delete the specified models
*
* @param array $ids
*
* @return int
*/
public function deleteMultipleById(array $ids);
/**
* Get the first specified model record from the database
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function first();
/**
* Get the first specified model record from the database
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrFail();
/**
* Get the specified models
*
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function get();
/**
* Get the specified model
*
* @param int $id
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function getById($id);
/**
* Add a group by clause to the query
*
* @param $columns
*
* @return $this
*/
public function groupBy($columns);
/**
* Add a limit clause to the query
*
* @param int $limit
*
* @return $this
*/
public function limit($limit);
/**
* Add an order by clause to the query.
*
* @param string $column
* @param string $direction
* @return $this
*/
public function orderBy($column, $direction = 'asc');
/**
* Update the specified model
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function updateById($id, array $data);
/**
* Add a basic where clause to the query.
*
* @param string $column
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function where($column, $operator = null, $value = null, $boolean = 'and');
/**
* Add a where in clause to the query
*
* @param $column
* @param $values
* @param string $boolean
* @param bool $not
*
* @return $this
*/
public function whereIn($column, $values, $boolean = 'and', $not = false);
/**
* Add a where not in clause to the query
*
* @param $column
* @param $values
* @param string $boolean
*
* @return $this
*/
public function whereNotIn($column, $values, $boolean = 'and');
/**
* Set relationships to eager load
*
* @param $relations
*
* @return $this
*/
public function with($relations);