sukohi/neatness
Composer 安装命令:
composer require sukohi/neatness
包简介
A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.
README 文档
README
A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.
(This is for Laravel 5+. For Laravel 4.2)
Installation
Execute composer command.
composer require sukohi/neatness:4.*
Preparation
At first, set NeatnessTrait in your Model.
use Sukohi\Neatness\NeatnessTrait;
class Item extends Eloquent {
use NeatnessTrait;
}
Secondary, add configuration values also in your Model.
default: Default key and direction. (Required)
columns: Keys and column names you want to sort. (Required)
symbols: Labels you will be able to use in your View. (Optional)
symbols: Symbols you will be able to use in your View. (Optional)
appends: Keys you want to append to URLs. (Optional)
protected $neatness = [
'default' => ['sort_id', 'desc'],
'columns' => [
'sort_id' => 'id',
'sort_title' => 'title',
'sort_date' => 'created_at'
],
'labels' => [
'sort_id' => 'ID',
'sort_title' => 'Title',
'sort_date' => 'Date'
],
'symbols' => [
'asc' => '<i class="fa fa-sort-asc"></i>',
'desc' => '<i class="fa fa-sort-desc"></i>',
'default' => '<i class="fa fa-sort"></i>'
],
'appends' => ['name']
];
Multiple columns: If you want to sort by multiple columns, you can use delimiter | like so.
'columns' => [
'id_n_title' => 'id|title'
],
Query Scope: You also can utilize Query Scopes instead of column name.
'columns' => [
'scope_title' => 'scope::sortTitle'
],
in this case, you need to prepare a scope method in your model. (About Query Scopes)
public function scopeSortTitle($query, $direction) {
return $query->orderBy('title', $direction);
}
Label: You can use label:: prefix to call a specific method.
'labels' => [
'title' => 'label::SortTitle'
],
in this case, you need to prepare a method in your model.
public function labelSortTitle() {
return 'Your Title'.
}
Usage
Now you can use a method called neatness.
(in Controller)
$items = Item::neatness()->get();
After call neatness(), you can access to sort data through $neatness.
(in View)
key: The key name sorting now.
Key: {{ $neatness->key }}
column: The column name sorting now.
Column: {{ $neatness->column }}
direction: The Direction sorting now. asc or desc
Direction: {{ $neatness->direction }}
urls: URLs to switch sort.
@foreach($neatness->urls as $key => $url)
{{ $key }} => {{ $url }}
@endforeach
or
$neatness->urls->get('title');
all_urls: All URLs to switch sort.
@foreach($neatness->all_urls as $key => $urls)
@foreach($urls as $direction => $url)
{{ $direction }} => {{ $url }}<br>
@endforeach
@endforeach
or
$neatness->all_urls->get('title'); // Array
$neatness->all_urls->get('title')['desc'] // URL
labels: Labels you set in your Model.
@foreach($neatness->labels as $key => $label)
{{ $key }} => {{ $label }}
@endforeach
or
$neatness->labels->get('title');
symbols: Symbols plucked with sort state.
@foreach($neatness->symbols as $key => $symbol)
{{ $key }} => {{ $symbol }}
@endforeach
or
$neatness->symbols->get('title');
texts: Texts mainly for link.
@foreach($neatness->urls as $key => $url)
<a href="{{ $url }}">{{ $neatness->texts->get($key) }}</a>
@endforeach
or
$neatness->texts->get('title');
appends: Array values for pagination
{{ $items->appends($neatness->appends)->links() }}
Change default column and direction
By this way, you can change default column and direction.
Item::neatness('title', 'desc')->get();
Relationship
You can use this package with relationship using join().
(in Controller)
$items = Item::join('item_details', 'item_details.item_id', '=', 'items.id')
->neatness()
->paginate(5);
(in Model)
protected $neatness = [
'default' => ['items.id', 'desc'],
'columns' => [
'id' => 'items.id',
'title' => 'items.title',
'date' => 'items.created_at',
'address' => 'item_details.address'
]
];
License
This package is licensed under the MIT License.
Copyright 2016 Sukohi Kuhoh
sukohi/neatness 适用场景与选型建议
sukohi/neatness 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.26k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sukohi/neatness 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sukohi/neatness 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 12.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-14