sukohi / smoothness
此软件包最新版本(4.0.6)没有提供许可证信息。
在此处输入您的软件包描述。
4.0.6
2016-07-14 10:28 UTC
Requires
- laravel/framework: ~5.0
README
一个用于管理 WHERE 子句的 Laravel 扩展包。
(适用于 Laravel 5+。 Laravel 4.2 版本)
安装
执行 composer 命令。
composer require sukohi/smoothness:4.*
准备工作
首先,在您的模型中设置 SmoothnessTrait
。
use Sukohi\Smoothness\SmoothnessTrait;
class Item extends Eloquent {
use SmoothnessTrait;
}
其次,在您的模型中添加配置值。
columns: 您想要使用的键和列名。(必需)
labels: 您想要使用的标签和键。(可选)
condition: 条件类型,可以是 and 或 or(可选,默认:auto)
protected $smoothness = [
'columns' => [
'search_id' => 'id',
'search_title' => 'title',
'search_date' => 'created_at'
],
'labels' => [
'search_id' => 'ID',
'search_title' => 'Item Title',
'search_date' => 'Date'
],
'condition' => 'and'
];
注意: 如果在 condition
中设置 auto
,则可以通过 URL 参数更改条件值,如下所示。
查询范围: 您也可以使用 查询范围
代替列名。
'columns' => [
'scope_title' => 'scope::filterTitle'
],
在这种情况下,您需要在您的模型中准备一个范围方法。(关于查询范围)
public function scopeFilterTitle($query, $value) {
return $query->where('title', $value);
}
标签: 您可以使用 label::
前缀来调用特定的方法。
'labels' => [
'title' => 'label::filterTitle'
],
在这种情况下,您需要在您的模型中准备一个方法。
public function labelFilterTitle() {
return 'Your Title'.
}
(您可能可以使用此提示来切换区域设置等。)
使用
现在,您可以使用名为 smoothness
的方法。
(在控制器中)
$items = Item::smoothness()->get();
调用 smoothness()
后,您可以通过 $smoothness
访问排序数据。
(在视图中)
condition: 当前条件。 and
和 or
。
Condition: {{ $smoothness->condition }}
values: 提交的数据。
@foreach($smoothness->values as $key => $value)
<input type="text" name="{{ $key }}" value="{{ $value }}">
@endforeach
or
$smoothness->values->get('KEY_NAME');
has_values: 有值的提交数据。
@foreach($smoothness->has_values as $column => $value)
{{ $column }} => {{ $value }}
@endforeach
or
$smoothness->has_values->get('KEY_NAME');
labels: 有值的列的数组值。
@foreach($smoothness->labels as $key => $label)
{{ $label }}
@endforeach
conditions: 有值的列的数组值。
@foreach($smoothness->conditions as $key => $boolean)
<input type="radio" name="condition" value="{{ $key }}"{{ ($boolean) ? ' checked' : '' }}>{{ ($key == 'and') ? 'And' : 'Or' }}
@endforeach
appends: 分页的数组值
{{ $items->appends($smoothness->appends)->links() }}
更改条件
第一个参数用于设置条件类型。
Item::smoothness('or')->get();
关联关系
您可以使用 join() 函数与关系一起使用此软件包。
(在控制器中)
$items = Item::join('item_details', 'item_details.item_id', '=', 'items.id')
->smoothness()
->paginate(5);
(在模型中)
protected $smoothness = [
'columns' => [
'items.id' => 'ID',
'items.title' => 'Title',
'items.created_at' => 'Date',
'item_details.address' => 'Address'
]
];
许可证
此软件包采用 MIT 许可证。
版权所有 2016 Sukohi Kuhoh