I want to create a report whose data can be grouped and ordered based on user requests
I have three tables
// 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: </option>
@endforeach
@foreach ($locations as $location)
<option value="location_id ">Location: </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;
}
I try to group by not grouped and ordered by all options and it works well, all data ordered by user request
but when I try to group by category: monitor and order by id (asc) and id (desc) appear only last data entered by user with category id = 1 which is id for monitor category as I showed below
[
{
"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"
}
]
it also happens if I group by other options and order by other options as well, what should I do?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire