我想创建一个报告,其数据可以根据用户请求进行分组和排序
我有三张桌子
// items
+----+------------+-------------+-------------+
| id | name | category_id | location_id |
+----+------------+-------------+-------------+
| 9 | Mouse 3 | 3 | 3 |
| 8 | Keyboard 3 | 2 | 3 |
| 7 | Monitor 3 | 1 | 3 |
| 6 | Mouse 2 | 3 | 2 |
| 5 | Keyboard 2 | 2 | 2 |
| 4 | Monitor 2 | 1 | 2 |
| 3 | Mouse 1 | 3 | 1 |
| 2 | Keyboard 1 | 2 | 1 |
| 1 | Monitor 1 | 1 | 1 |
+----+------------+-------------+-------------+
// item_categories
+----+----------+
| id | name |
+----+----------+
| 3 | Mouse |
| 2 | Keyboard |
| 1 | Monitor |
+----+----------+
// item_locations
+----+--------+
| id | name |
+----+--------+
| 3 | Room 3 |
| 2 | Room 2 |
| 1 | Room 1 |
+----+--------+
items.blade.php
<div class="input-field">
<select name="groupBy">
<option value="not grouped">Not Grouped - Default</option>
@foreach ($categories as $category)
<option value="category_id {{ $category->id }}">Category: {{ $category->name }}</option>
@endforeach
@foreach ($locations as $location)
<option value="location_id {{ $location->id }}">Location: {{ $location->name }}</option>
@endforeach
</select>
<label>Group by</label>
</div>
<div class="input-field">
<select name="orderBy">
<option value="id asc">Id (Asc) - Default</option>
<option value="id desc">Id (Desc)</option>
<option value="name asc">Name (A - Z)</option>
<option value="name desc">Name (Z - A)</option>
<option value="updated_at desc">Date (Newest)</option>
<option value="updated_at asc">Date (Oldest)</option>
</select>
<label>Order by</label>
</div>
ItemController
public function create()
{
$items = Item::Join('item_categories', 'items.category_id', '=', 'item_categories.id')
->Join('item_locations', 'items.location_id', '=', 'item_locations.id')
->select('items.*', 'item_categories.name as category_name', 'item_locations.name as location_name')
->orderBy('updated_at', 'desc')
->get();
$categories = ItemCategories::all();
$locations = ItemLocation::all();
return view('menu.items', compact('items', 'categories', 'locations'));
}
ReportController
public function items(Request $request)
{
$date = Carbon::now()->toFormattedDateString();
$orderByArray = explode(' ', request('orderBy'));
$orderColumn = $orderByArray[0];
$orderDirection = $orderByArray[1];
$groupByArray = explode(' ', request('groupBy'));
$groupColumn = $groupByArray[0];
$groupDirection = $groupByArray[1];
if (request('groupBy') == 'not grouped') {
$items = Item::Join('item_categories', 'items.category_id', '=', 'item_categories.id')
->Join('item_locations', 'items.location_id', '=', 'item_locations.id')
->select('items.*', 'item_categories.name as category_name', 'item_locations.name as location_name')
->orderBy($orderColumn, $orderDirection)
->get();
} else {
$items = Item::Join('item_categories', 'items.category_id', '=', 'item_categories.id')
->Join('item_locations', 'items.location_id', '=', 'item_locations.id')
->select('items.*', 'item_categories.name as category_name', 'item_locations.name as location_name')
->orderBy($orderColumn, $orderDirection)
->groupBy($groupColumn)
->having($groupColumn, '=', $groupDirection)
->get();
}
return $items;
}
我尝试进行分组,不按所有选项进行分组和排序,效果很好,按用户请求排序的所有数据
但是当我尝试按类别分组时:监视器和按ID(asc)和id(desc)排序只显示用户输入的最后数据,类别为id = 1,这是监视器类别的ID,如下所示
[
{
"id": 7,
"category_id": "1",
"location_id": "3",
"name": "Monitor 3",
"quantity": "0",
"created_at": "2018-02-11 06:40:08",
"updated_at": "2018-02-11 06:40:08",
"category_name": "Monitor",
"location_name": "Room 3"
}
]
如果我按其他选项分组并按其他选项排序,也会发生这种情况,我该怎么办?
答案 0 :(得分:0)
您需要将其更改为:->select('items.*', 'item_categories.name as category_name', 'item_locations.name as location_name', 'items.location_id as location_id', 'items.category_id as category_id')
您需要包含这两个字段,因为您要按其中一个字段进行分组,这样它就可以同时发送location_id
或{{1 }}