我想显示我的行,但是我想将它们分为几部分,并按其类型ID进行分组。
Section 1
---------
<item>
<item>
Section 2
---------
<item>
我可以轻松地在刀片视图中进行操作,如下所示...
@foreach($types as $type)
<h1>{{ $type->name }}</h1>
<div class="list">
<ul>
@forelse(App\Equipment::where("type_id", $type->id)->get() as $item)
<li>
{{ $item->name }}
</li>
@empty
<li>No results found.</li>
@endforelse
</ul>
</div>
@endforeach
但是这无效,我需要在控制器中实现查询,因为我需要查询参数。
public function index(Request $request) {
$item = \DB::table("equipment");
$appends = [];
foreach(["type_id", "sockets", "quality_id", "one_hand"] as $field) {
if($request->has($field)) {
$appends[$field] = $request->input($field);
$item->where($field, $request->input($field));
}
}
if($request->has("class")) {
$appends["class"] = $request->input("class");
$item->where(["class_id" => $request->input("class")])
->orWhere(["class_id2" => $request->input("class")], ["class_id3" => $request->input("class")]);
}
if($request->has("name")) {
$appends["name"] = $request->input("name");
$item->where("name", "like", "%" . $request->input("name") . "%");
}
if($request->has("stats")) {
$stats = $request->input("stats");
$appends["stats"] = $stats;
foreach($stats as $stat) {
$item->where("stat_1", $stat)
->orWhere("stat_2", $stat)
->orWhere("stat_3", $stat)
->orWhere("stat_4", $stat)
->orWhere("stat_5", $stat)
->orWhere("stat_6", $stat)
->orWhere("stat_7", $stat)
->orWhere("stat_8", $stat)
->orWhere("stat_9", $stat)
->orWhere("stat_10", $stat)
->orWhere("stat_11", $stat);
}
}
$item->groupBy("type_id");
$items = $item->paginate(15);
$items->appends($appends);
return view("equipment.index", [
"items" => $items,
"types" => Types::all(),
"stats" => EquipmentStats::all(),
"classes" => CLasses::all(),
"qualities" => EquipmentQualities::all()
]);
}
如何在我的控制器中实现?
答案 0 :(得分:0)
我猜您正在寻找的是types
和equipments
。因此,请使用紧急加载。
控制器
public function index()
{
$types = Type::with(['equipments' => $this->searchScope()])->get();
return view("equipment.index", [
"types" => $types,
... // other things
]);
}
protected function searchScope()
{
return function($query) {
$request = request();
$appends = [];
foreach(["type_id", "sockets", "quality_id", "one_hand"] as $field) {
if($request->has($field)) {
$appends[$field] = $request->input($field);
$query->where($field, $request->input($field));
}
}
if($request->has("class")) {
$appends["class"] = $request->input("class");
$query->where(["class_id" => $request->input("class")])
->orWhere(["class_id2" => $request->input("class")], ["class_id3" => $request->input("class")]);
}
if($request->has("name")) {
$appends["name"] = $request->input("name");
$query->where("name", "like", "%" . $request->input("name") . "%");
}
if($request->has("stats")) {
$stats = $request->input("stats");
$appends["stats"] = $stats;
foreach($stats as $stat) {
$query->where("stat_1", $stat)
->orWhere("stat_2", $stat)
->orWhere("stat_3", $stat)
->orWhere("stat_4", $stat)
->orWhere("stat_5", $stat)
->orWhere("stat_6", $stat)
->orWhere("stat_7", $stat)
->orWhere("stat_8", $stat)
->orWhere("stat_9", $stat)
->orWhere("stat_10", $stat)
->orWhere("stat_11", $stat);
}
}
$query->groupBy("type_id");
};
}
查看
@foreach($types as $type)
<h1>{{ $type->name }}</h1>
<div class="list">
<ul>
@forelse($type->equipments as $equipment)
<li>
{{ $equipment->name }}
</li>
@empty
<li>No results found.</li>
@endforelse
</ul>
</div>
@endforeach
注意
为此,您需要在equipments
模型上定义Type
关系。
如果您没有。这就是方法。
类型模型
class Type extends Model
{
public function equipments()
{
return $this->hasMany(Equipment:class);
}
}
答案 1 :(得分:0)
使用const asyncFunc = (a) => {
return new Promise((resolve) => {
setTimeout(() => resolve(a), 1000);
})
}
const asyncFunc1 = () => asyncFunc(1);
const asyncFunc2 = () => asyncFunc(2);
async function foo() {
const result1 = asyncFunc1();
const result2 = asyncFunc2();
return JSON.stringify({ result1, result2 });
}
async function foo2() {
return [result1, result2] = await Promise.all([
asyncFunc1(),
asyncFunc2(),
]);
}
(async () =>{
const el = document.createElement('div');
el.appendChild(document.createTextNode(await foo()));
el.appendChild(document.createTextNode(await foo2()));
document.querySelector('body').appendChild(el);
})();
// {"result1":{},"result2":{}} 1,2
按groupBy
对设备进行分组
type_id
然后您认为
$items = $item->paginate(15);
$itemsCollection = $items->groupBy(function($item) {
return $item->type_id;
});
$items = new \Illuminate\Pagination\LengthAwarePaginator($itemsCollection, $items->total(), $items->perPage());
$items->appends($appends);