exyplis / eloquent-builder-macros
一套针对 Laravel Eloquent 查询构建器的实用宏
Requires
- illuminate/database: >=5.4 < 9.0
- illuminate/support: >=5.4 < 9.0
Requires (Dev)
Suggests
- spatie/laravel-collection-macros: A set of useful Laravel collection macros.
README
此包包含一些由 Exyplis 开发团队精心挑选的实用 Eloquent 构建器宏。我们发现这些宏在日常开发中非常有用,因为我们喜欢干净、可读且易于维护的代码。
兼容 Laravel v5.4+。
附言:如果您有针对 Laravel Eloquent Builder 的任何有用的宏,但未在此展示,请随时将其添加到我们的集合中,我们非常感谢您的支持,并乐意合并它 🤝
安装
您可以通过 composer 安装此包
composer require exyplis/eloquent-builder-macros
Laravel 5.4
将此条目添加到您的 config/app.php
文件中的 providers
数组。
Exyplis\EloquentBuilderMacros\EloquentBuilderMacrosServiceProvider::class
Laravel 5.5+
包将自动注册自身,因此您无需执行其他操作。
可用宏
notEmptyWhere
notEmptyOrWhere
notEmptyWhereIn
notEmptyOrWhereIn
notEmptyWhereNotIn
notEmptyOrWhereNotIn
notEmptyOrWhereTime
notEmptyOrWhereDate
notEmptyOrWhereMonth
notEmptyOrWhereYear
if
addSubSelect
orderBySub
orderBySubDesc
searchIn
notEmptyWhere
检查传递的参数是否为空,如果不为空,则在现有查询上添加对 $column
的 where
条件。这在您有复杂查询且包含许多结构时非常有用
签名
notEmptyWhere($column,$param)
示例
- Model::when($request->has('key'), function($query){ - return $query->where('column',$request->input('key'); - })->get(); + Model::notEmptyWhere('column',$request->input('key'))->get();
notEmptyOrWhere
检查传递的参数是否为空,如果不为空,则在现有查询上添加对 $column
的 orWhere
条件。这在您有复杂查询且包含许多结构时非常有用
签名
notEmptyOrWhere($column,$param)
示例
- Model::when($request->has('key'), function($query){ - return $query->orWhere('column',$request->input('key'); - })->get(); + Model::notEmptyOrWhere('column',$request->input('key'))->get();
notEmptyWhereIn
检查传递的参数是否为空,如果不为空,则在现有查询上添加对 $column
的 whereIn
条件。在这种情况下,$param
应该是数组。
签名
notEmptyWhereIn($column,$params)
示例
- Model::when($request->has('user_ids'), function($query){ - return $query->whereIn('user_id', $request->input('user_ids'); - })->get(); + Model::notEmptyWhereIn('column',$request->input('user_ids'))->get()
notEmptyOrWhereIn
检查传递的参数是否为空,如果不为空,则在现有查询上添加对 $column
的 orWhereIn
条件。在这种情况下,$param
应该是数组。
签名
notEmptyOrWhereIn($column,$params)
示例
- Model::when($request->has('user_ids'), function($query){ - return $query->orWhereIn('user_id', $request->input('user_ids'); - })->get(); + Model::notEmptyOrWhereIn('column',$request->input('user_ids'))->get()
notEmptyWhereNotIn
检查传入的参数是否为空,如果不为空,则在现有的查询上对$column
添加whereNotIn
条件。在这种情况下,$param
应该是数组。
函数签名
notEmptyWhereNotIn($column,$params)
示例
- Model::when($request->has('user_ids'), function($query){ - return $query->whereNotIn('user_id', $request->input('user_ids'); - })->get(); + Model::notEmptyWhereNotIn('column',$request->input('user_ids'))->get()
notEmptyOrWhereNotIn
检查传入的参数是否为空,如果不为空,则在$column
上对现有的查询添加orWhereNotIn
条件。在这种情况下,$param
应该是数组。
函数签名
notEmptyOrWhereNotIn($column,$params)
示例
- Model::when($request->has('user_ids'), function($query){ - return $query->orWhereNotIn('user_id', $request->input('user_ids'); - })->get(); + Model::notEmptyOrWhereNotIn('column',$request->input('user_ids'))->get()
notEmptyWhereTime
检查传入的参数是否为空,如果不为空,则在$column
上对现有的查询添加whereTime
条件。这在有复杂查询且包含许多结构时很有用。
函数签名
notEmptyWhereTime($column,$param)
示例
- Model::when($request->has('key'), function($query){ - return $query->whereTime('column',$request->input('key'); - })->get(); + Model::notEmptyWhereTime('column',$request->input('key'))->get();
notEmptyWhereDate
检查传入的参数是否为空,如果不为空,则在$column
上对现有的查询添加whereDate
条件。这在有复杂查询且包含许多结构时很有用。
函数签名
notEmptyWhereDate($column,$param)
示例
- Model::when($request->has('key'), function($query){ - return $query->whereDate('column',$request->input('key'); - })->get(); + Model::notEmptyWhereDate('column',$request->input('key'))->get();
notEmptyWhereMonth
检查传入的参数是否为空,如果不为空,则在$column
上对现有的查询添加whereMonth
条件。这在有复杂查询且包含许多结构时很有用。
函数签名
notEmptyWhereMonth($column,$param)
示例
- Model::when($request->has('key'), function($query){ - return $query->whereMonth('column',$request->input('key'); - })->get(); + Model::notEmptyWhereMonth('column',$request->input('key'))->get();
notEmptyWhereYear
检查传入的参数是否为空,如果不为空,则在$column
上对现有的查询添加whereYear
条件。这在有复杂查询且包含许多结构时很有用。
函数签名
notEmptyWhereYear($column,$param)
示例
- Model::when($request->has('key'), function($query){ - return $query->whereYear('column',$request->input('key'); - })->get(); + Model::notEmptyWhereYear('column',$request->input('key'))->get();
if
检查传入的条件,当条件返回true
时,向查询添加自定义的where
子句。
函数签名
if($condition, $column, $operator, $value)
示例
- Model::when($request->customer_id, function($query) use ($request){ - return $query->where('customer_id', $request->customer_id); - })->get(); + Model::if($request->customer_id, 'customer_id', '=', $request->customer_id)->get()
addSubSelect
函数签名
// this should be documented.
示例
-Before +After
orderBySub
函数签名
// this should be documented
示例
-Before +After
orderBySubDesc
签名
// this should be documented.
示例
-Before +After
searchIn
签名
searchIn($attributes, $needle)
示例
// Get row, where name or email contains `john` Model::search(['name','email',], 'john')->get();
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全
如果您发现任何安全相关的问题,请发送电子邮件至 bks@exyplis.com,而不是使用问题跟踪器。
致谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅许可文件。