Laravel Eloquent group通过返回每个组的一个标题 [英] Laravel Eloquent groupBy to return one title of each group
问题描述
我遇到了一个非常复杂的问题,如果每个项目的标题都相同,我想在一个标题下显示项目,我是Laravel的新手,并且不知道该怎么做,我希望对此问题有个真正的支持,下面是结果的屏幕截图,请帮助我提供一个非常详细的解决方案来解决此问题,我已经苦苦解决了一周,但无法做到这一点
I have a very complex issue, I want to show items under one title if title is the same for every item, Im new to Laravel and dont know how to do this, I hope there is a real soution for this issue, below screen shot of the result, please help me in a very detailed solution to solve this issue, im suffering since a week to solve it but cant do this
screen shot:
表格:
控制器:
$customizeorders = OrderCustomize::where('userorder_id',$order_number)->with('customizeproduct')->get()->groupBy('customizetitle_id');
OrderCustomize模型:
OrderCustomize Model:
protected $table = "ordercustomizes";
protected $fillable = [
'customizeproduct_id',
'userorder_id',
'product_id'
];
public function customizeproduct()
{
return $this->belongsTo(CustomizeProduct::class);
}
CustomizeProduct型号:
CustomizeProduct Model:
protected $table = "customizeproducts";
protected $fillable = [
'customizetitle_id',
'product_id',
'selection_type',
'selection_number',
'customize_title',
'customize_price'
];
public function customizetitle()
{
return $this->belongsTo(CustomizeTitle::class);
}
刀片:
@if(count($customizeorders)>0)
@foreach ($customizeorders as $customizetitle => $groupCustomizes)
@foreach($groupCustomizes as $key=>$customize)
@if(($userorder->product_id)==($customize->product_id))
<div>{{$customize->customizeproduct->customizetitle->name}}:</div>
{{$customize->customizeproduct->customize_title}} .
@endif
@endforeach
@break
@endforeach
@endif
推荐答案
//you need to create array to store the array of items having same title.
//i guess the item having same title also have same id.
Then you should use order by id not group by.
//it will provide you the data of same id in the conitnuous queue
//then you can use foreach loop to push the group of items in array and wrap that array with another array.
//so that you can push that in view. then you can loop through the array in view get the desired data.
//here in this method it have check if the items belongs to previous items or not using the $previousItem variable
public function show_device_report(Request $request)
{
//store the overall report.
$wholeData = [];
//stores the individual wrapper.
$wrapper = [];
//getting the id for the first items in the list
$previousItem = $datas[0]->id;
//transforming the values before sending to the view by
foreach ($datas as $data) {
//store the report of every items
$singleItem = [];
if ($data->id == $previousItem) {
$id = $data->id
//get all the item you need and push in the array.
array_push($singleItem, $id, $name, $etc);
array_push($wrapper, $singleItem);
} else {
$previousItem = $data->id;
array_push($wholeData, $wrapper);
//set the wrapper to null so that it can store the another items group in another index.
$wrapper= [];
}
return $wholeData ;
}
您需要根据需要修改一些代码.由于您没有提供任何表结构.将单个项目存储在singleItem []中,然后将所有单个项目存储在wrapper []中,并将所有wraper []存储在Wholedata []中.发送Wholedata []到视图.
You need to modify little code according to your needs. As there is no any table structure provided by you. store the individual item in singleItem[], then store all the single items in wraper[], and all the wraper[] in wholedata[]. Send wholedata[] to the view.
whole[
[wrapper1[item1,item2,item3]],
[wrapper2[item1,item2,item3,item4]],
[wrapper3[item1,item2]],
]
这篇关于Laravel Eloquent group通过返回每个组的一个标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!