Laravel 5.8 背包 addColumn 自定义类型
我们正在接管一个站点,在重建它之前,我们需要使用
- Laravel 5.8
- Backpack crud 3.6
修复一些问题仪表板显示来自以下位置的所有用户(id、姓名、电子邮件) MySql users 表,直接使用
$this->crud->setFromDb();
创建的另一个列来显示与存储在与 users 表不同的表中的订阅相关的用户状态(活动/非活动)。 此列使用带有自定义列类型的 addColumn 方法,如下所示:
$columnStat = [
'label' => "Status",
'type' => 'user_status',
'name' => 'id', // the db column for the foreign key
'key' => 'user_status', // this key helps to differentiate between column using same name
'entity' => 'subscriptions', // the method that defines the relationship in your Model
'attribute' => 'user_id', // foreign key attribute that is shown to user
'model' => "App\Models\UserSubscriptions" // foreign key model
];
$this->crud->addColumn($columnStat);
自定义类型在 Blade 文件中定义 backpack/crud/columns/user_status.blade.php
@php
$isActive = false;
$user_id = $entry->{$column['name']};
if(isset($user_id) && is_int($user_id)) {
$subscription = \App\Models\UserSubscription::where('end_date', '>' , Carbon\Carbon::today())
->where('payment_status', '=' , 1)
->where('user_id', '=' , $user_id)
->get()->last();
if($subscription){
$isActive = true;
}
}
@endphp
{{-- regular object attribute --}}
@if($isActive)
<td>Active</td>
@else
<td>Inactive</td>
@endif
: user_subcriptions 的 MySql 表如下:
mysql> desc user_subscriptions;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| subscription | text | YES | | NULL | |
| start_date | date | YES | | NULL | |
| end_date | date | YES | | NULL | |
| user_id | int unsigned | NO | MUL | NULL | |
| payment_status | tinyint(1) | NO | | 0 | |
| deleted_at | timestamp | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
一切正常(活动和非活动均正确显示),除了列无法按升序或降序排序 命令。
Backpackforlaravel 文档显示了一个有关如何改进该列以获取自定义订单的示例: (在互联网上搜索:backpackforlaravel.com 和 custom-order-logic-for-columns)
$this->crud->addColumn([
// Select
'label' => 'Category',
'type' => 'select',
'name' => 'category_id', // the db column for the foreign key
'entity' => 'category', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'orderable' => true,
'orderLogic' => function ($query, $column, $columnDirection) {
return $query->leftJoin('categories', 'categories.id', '=', 'articles.select')
->orderBy('categories.name', $columnDirection)->select('articles.*');
}
]);
我只是不明白如何在我的案例中实现这一点。
我的问题:
- 如何修改代码以使状态列可按升序或降序排序?
- 有没有办法让我输出/记录列的查询,以便我可以了解如何修改它?
任何帮助将不胜感激。
We are taking over a site and before rebuilding it we need to fix a few issues using
- Laravel 5.8
- Backpack crud 3.6
The dashboard shows all users (id, name, email) from the MySql users table, which is straight forward using
$this->crud->setFromDb();
Another column is created to display the status (active/inactive) of the user in relation to subscriptions stored in a different table as the users table.
This column uses the addColumn method with a custom column type like so:
$columnStat = [
'label' => "Status",
'type' => 'user_status',
'name' => 'id', // the db column for the foreign key
'key' => 'user_status', // this key helps to differentiate between column using same name
'entity' => 'subscriptions', // the method that defines the relationship in your Model
'attribute' => 'user_id', // foreign key attribute that is shown to user
'model' => "App\Models\UserSubscriptions" // foreign key model
];
$this->crud->addColumn($columnStat);
The custom type is defined in a blade file backpack/crud/columns/user_status.blade.php:
@php
$isActive = false;
$user_id = $entry->{$column['name']};
if(isset($user_id) && is_int($user_id)) {
$subscription = \App\Models\UserSubscription::where('end_date', '>' , Carbon\Carbon::today())
->where('payment_status', '=' , 1)
->where('user_id', '=' , $user_id)
->get()->last();
if($subscription){
$isActive = true;
}
}
@endphp
{{-- regular object attribute --}}
@if($isActive)
<td>Active</td>
@else
<td>Inactive</td>
@endif
The MySql table for user_subcriptions is the following:
mysql> desc user_subscriptions;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| subscription | text | YES | | NULL | |
| start_date | date | YES | | NULL | |
| end_date | date | YES | | NULL | |
| user_id | int unsigned | NO | MUL | NULL | |
| payment_status | tinyint(1) | NO | | 0 | |
| deleted_at | timestamp | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
This is all working fine (active and inactive are displayed correctly) except that the column can't be sorted in ascending or descending order.
The Backpackforlaravel docs show an example on how to improve the column to get a custom order:
(search internet for: backpackforlaravel.com and custom-order-logic-for-columns)
$this->crud->addColumn([
// Select
'label' => 'Category',
'type' => 'select',
'name' => 'category_id', // the db column for the foreign key
'entity' => 'category', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'orderable' => true,
'orderLogic' => function ($query, $column, $columnDirection) {
return $query->leftJoin('categories', 'categories.id', '=', 'articles.select')
->orderBy('categories.name', $columnDirection)->select('articles.*');
}
]);
I just don't understand how to implement this here in my case.
My questions:
- How can I modify the code to make the status column sortable in ascending or descending order?
- is there a way for me to output/log the query of the column so that I can understand how to modify it?
Any help would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要查看列后面的查询并记录它,请查看此处给出的详细答案:如何在 Backpack 中查看列表视图后面的 AJAX 查询?。要添加到之前的答案,如果您有 Laravel Debugbar (https://github.com/barryvdh/laravel -debugbar) 安装后,您可以使用以下命令显示 Backpack 列查询: \Debugbar::info(asSql($this->crud->query));
To view the query behind the column and log it, view the detailed answer given here: How to see AJAX query behind list view in Backpack?. To add to this previous answer, if you have Laravel Debugbar (https://github.com/barryvdh/laravel-debugbar) installed you can display the Backpack column query there using this: \Debugbar::info(asSql($this->crud->query));