【问题标题】:Laravel Eloquent groupBy to return one title of each groupLaravel Eloquent groupBy 返回每个组的一个标题
【发布时间】:2019-09-11 05:05:55
【问题描述】:

我有一个非常复杂的问题,如果每个项目的标题都相同,我想在一个标题下显示项目,我是 Laravel 新手,不知道该怎么做,我希望这个问题有一个真正的解决方案,下面是结果的屏幕截图,请帮助我提供一个非常详细的解决方案来解决这个问题,我花了一周的时间来解决这个问题,但不能这样做

 screen shot: 

https://imgur.com/IOLGpzU

表格:

https://imgur.com/AbVNDqP

https://imgur.com/kQTzQA2

https://imgur.com/brGYl54

https://imgur.com/oLXjGl9

控制器:

 $customizeorders = OrderCustomize::where('userorder_id',$order_number)->with('customizeproduct')->get()->groupBy('customizetitle_id');

OrderCustomize 模型:

  protected $table = "ordercustomizes";
     protected $fillable = [        
   'customizeproduct_id',
    'userorder_id',
    'product_id'
];

 public function customizeproduct()
  {
    return $this->belongsTo(CustomizeProduct::class);
 }

自定义产品型号:

  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

【问题讨论】:

  • 您面临的问题是什么?
  • 您期望什么输出?什么问题。

标签: php laravel


【解决方案1】:

//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[] 中,然后将所有单个项目存储在 wraper[] 中,并将所有 wraper[] 存储在 Wholedata[] 中。将 Wholedata[] 发送到视图。
whole[
[wrapper1[item1,item2,item3]],
[wrapper2[item1,item2,item3,item4]],
[wrapper3[item1,item2]],
]

【讨论】:

  • 无法理解数组的存储方式和存储位置?
  • 我为你添加了一些提示。并编辑了答案
  • 我有很多表我不知道如何将多个表中的项目存储到数组中,我可以提供表吗?
  • 我添加了 4 个表格,用于显示标题及其项目
  • 您可以使用连接查询的集合对象以数组形式检索数据,然后在其中使用循环。意思是在$customizeorders变量中获取数据后,使用该变量来操作foreach循环
猜你喜欢
  • 2020-12-03
  • 2016-12-07
  • 2013-09-03
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多