kyslik / column-sortable
Laravel 6.x 处理列排序的包
Requires
- php: >=7.2
- illuminate/database: 5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: 5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^5.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^8.5|^9.5.10|^10.5
- dev-master
- 6.6.0
- 6.5.0
- 6.4.2
- 6.4.1
- 6.4.0
- 6.3.0
- 6.2.1
- 6.2.0
- 6.1.0
- 6.0.0
- 5.8.0
- 5.7.0
- 5.6.3
- 5.6.2
- 5.6.1
- 5.6.0
- 5.5.8
- 5.5.7
- 5.5.6
- 5.5.5
- 5.5.4
- 5.5.3
- 5.5.2
- 5.5.1
- 5.5.0
- 5.4.11
- 5.4.10
- 5.4.9
- 5.4.8
- 5.4.7
- 5.4.6
- 5.4.5
- 5.4.4
- 5.4.3
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.10
- 5.3.9
- 5.3.8
- 5.3.7
- 5.3.6
- 5.3.5
- 5.3.4
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.10
- 5.2.9
- 5.2.8
- 5.2.7
- 5.2.6
- 5.2.5
- 5.2.4
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.14
- 5.1.13
- 5.1.12
- 5.1.11
- 5.1.10
- 5.1.9
- 5.1.8
- 5.1.7
- 5.1.6
- 5.1.5
- 5.1.4
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.0.1
- 4.0.0
- 3.0.6
- 3.0.0
- 2.0.2
- 2.0.0
- dev-healyhatman-custom-hrefs
- dev-L6
- dev-L6.1
- dev-L6.0
- dev-L5.8
- dev-L5.5-7
- dev-L5.5-6
- dev-L5.4
- dev-L5.1-3
- dev-L5.5
- dev-L5.1
- dev-L5.2
- dev-L5.3
This package is auto-updated.
Last update: 2024-08-30 00:05:23 UTC
README
Laravel 5.[5-8] 版本的列排序包。对于更早版本的 Laravel,请检查 L5.1-3 分支
设置
Composer
通过 Composer 添加此包(开发/最新版本 dev-master
)
{ "require": { "kyslik/column-sortable": "^6.0" } }
composer update
Laravel >=5.5 自动发现
简单地安装包,让 Laravel 做其魔法。
注意
(Laravel 6.0 之前): 主版本和次版本应与 Laravel 的版本匹配,例如,如果您正在使用 Laravel 5.4,column-sortable 版本应为 5.4.*
。
手动安装(5.5 之前)
将服务提供者添加到 config/app.php
中的提供者数组
'providers' => [ App\Providers\RouteServiceProvider::class, /* * Third Party Service Providers... */ Kyslik\ColumnSortable\ColumnSortableServiceProvider::class, ],
发布配置
将包配置文件发布到您的应用程序
php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider" --tag="config"
查看配置文件 (config/columnsortable.php
) 并根据需要进行调整。
使用方法
在您的 Eloquent 模型中内部使用 Sortable 特性。定义 $sortable
数组(见以下示例代码)。
注意
Scheme::hasColumn()
仅在未定义 $sortable
时运行 - 每个请求的数据库访问次数更少。
use Kyslik\ColumnSortable\Sortable; class User extends Model implements AuthenticatableContract, CanResetPasswordContract { use Authenticatable, CanResetPassword, Sortable; ... public $sortable = ['id', 'name', 'email', 'created_at', 'updated_at']; ... }
您已准备就绪。
Sortable 特性为模型添加了 Sortable 范围,因此您可以使用它与 paginate 一起使用。
Blade 扩展
您可以使用 @sortablelink() blade 扩展。
@sortablelink('column', 'Title', ['parameter' => 'smile'], ['rel' => 'nofollow'])
Column(第 1 个)参数是数据库中的列,Title(第 2 个)参数是在锚点标签内显示的,array()
参数(第 3 个)是默认(GET)查询字符串参数,array()
参数(第 4 个)是用于附加到锚点标签的额外属性。您可以在第 4 个参数中使用自定义 URL 作为 'href' 属性,并将查询字符串附加到它。
您可以选择省略第 2、第 3 和第 4 个参数。
Blade 扩展的示例和用法
@sortablelink('name') @sortablelink('name', 'Username') @sortablelink('address', trans('fields.address'), ['filter' => 'active, visible']) @sortablelink('address', trans('fields.address'), ['filter' => 'active, visible'], ['class' => 'btn btn-block', 'rel' => 'nofollow', 'href' => route('my.custom.route')])
如果您不填写 Title(第 2 个参数),则使用列名称。
注意
您可以为 Title(第 2 个参数)设置默认格式化函数,默认设置为 ucfirst
。
简要说明
Sortablelink blade 扩展区分 类型(numeric、amount 和 alpha)并为每种类型应用不同的类。
查看以下片段
'columns' => [ 'numeric' => [ 'rows' => ['created_at', 'updated_at', 'level', 'id'], 'class' => 'fa fa-sort-numeric' ], 'amount' => [ 'rows' => ['price'], 'class' => 'fa fa-sort-amount' ], 'alpha' => [ 'rows' => ['name', 'description', 'email', 'slug'], 'class' => 'fa fa-sort-alpha', ], ],
其余的 配置文件 应该很清晰,我建议您快速浏览。
Font Awesome(默认字体类)
安装 Font-Awesome 以获得视觉 Joy。在 cheatsheet 中搜索 "sort" 并查看自己使用的图标(12)。
Font Awesome 5
将配置文件中的后缀类从 -asc
/-desc
(FA 4)分别更改为 -up
/-down
(FA 5)。
/* this is FA 5 compatible. suffix class that is appended when ascending direction is applied */ 'asc_suffix' => '-up', /* suffix class that is appended when descending direction is applied */ 'desc_suffix' => '-down',
注意
如果您还没有发布配置,请遵循上面的说明。
完整示例
您可能对工作示例仓库感兴趣,其中演示了包的使用。
路由
Route::get('users', ['as' => 'users.index', 'uses' => 'HomeController@index']);
控制器 index()
方法
public function index(User $user) { $users = $user->sortable()->paginate(10); return view('user.index')->withUsers($users); }
您可以为空URL设置默认排序参数。
例如:页面首次加载时,默认方向是可配置的(升序)
$users = $user->sortable('name')->paginate(10); // produces ->orderBy('users.name', 'asc') $users = $user->sortable(['name'])->paginate(10); // produces ->orderBy('users.name', 'asc') $users = $user->sortable(['name' => 'desc'])->paginate(10); // produces ->orderBy('users.name', 'desc')
视图(包含分页)
@sortablelink('id', 'Id') @sortablelink('name') @foreach ($users as $user) {{ $user->name }} @endforeach {!! $users->appends(\Request::except('page'))->render() !!}
注意
Blade识别指令的能力取决于指令本身之前是否有空格 <tr> @sortablelink('Name')
HasOne / BelongsTo 关系排序
定义 hasOne 关系
为了使关系排序正常工作,您必须在模型中定义hasOne()
关系。
/** * Get the user_detail record associated with the user. */ public function detail() { return $this->hasOne(App\UserDetail::class); }
定义 belongsTo 关系
注意
如果是自引用模型(如评论、分类等);父表将使用parent_
字符串进行别名,更多信息请参阅问题 #60。
/** * Get the user that owns the phone. */ public function user() { return $this->belongsTo(App\User::class); }
在User模型中,我们定义了与UserDetail模型(包含电话号码和地址详情)的hasOne
关系。
定义 $sortable
数组
在两个模型中定义$sortable
数组(否则,包将使用Scheme::hasColumn()
,这会产生额外的数据库查询)。
对于User
public $sortable = ['id', 'name', 'email', 'created_at', 'updated_at'];
对于UserDetail
public $sortable = ['address', 'phone_number'];
Blade 和关系排序
为了告诉包使用关系进行排序
@sortablelink('detail.phone_number', 'phone') @sortablelink('user.name', 'name')
注意
包使用您在模型中定义的“name”关系(方法)而不是表名。
警告
不要同时使用两个不同的关系,否则您会得到错误信息,即关系未定义
在配置文件中,您可以设置自己的分隔符,如果点(点)不是您想要的。
'uri_relation_column_separator' => '.'
ColumnSortable 覆盖(高级)
您可以使用自己的连接(查询)覆盖ColumnSortable关系功能,基本上您可以编写自己的连接(查询)并手动应用orderBy()
。
见示例
class User extends Model { use Sortable; public $sortable = ['name']; ... public function addressSortable($query, $direction) { return $query->join('user_details', 'users.id', '=', 'user_details.user_id') ->orderBy('address', $direction) ->select('users.*'); } ...
控制器相同$users = $user->sortable()->paginate(10);
在视图中使用@sortablelink('address')
别名
您可以声明$sortableAs
数组并使用它进行别名(绕过列存在检查),并忽略以表为前缀。
在模型中
... $sortableAs = ['nick_name']; ...
在控制器中
$users = $user->select(['name as nick_name'])->sortable(['nick_name'])->paginate(10);
在视图中
@sortablelink('nick_name', 'nick')
有关别名更多信息,请参阅#44。
使用 withCount()
别名在您想要使用withCount()
对结果进行排序时很有用,有关更多信息,请参阅问题 #49。
异常捕获
包抛出自定义异常ColumnSortableException
,带有三个代码(0,1,2)。
代码0
表示explode()
无法将URI参数“sort”分解为两个值。例如:sort=detail..phone_number
- 产生大小为3的数组,导致包抛出带有代码0
的异常。
代码1
表示$query->getRelation()
方法失败,这意味着关系名称无效(不存在,未在模型中声明)。
代码2
表示通过排序参数提供的关联不是hasOne
的实例。
如何捕获示例
... try { $users = $user->with('detail')->sortable(['detail.phone_number'])->paginate(5); } catch (\Kyslik\ColumnSortable\Exceptions\ColumnSortableException $e) { dd($e); }
注意
我强烈建议捕获ColumnSortableException
,因为存在用户输入(GET参数),任何用户都可以修改它,使包抛出带有代码0
的ColumnSortableException。