首页 > 解决方案 > Laravel,如果商店具有相同的组ID,则获取一组商店的所有订单

问题描述

我陷入了一个非常复杂的问题,我有商店、组和订单表我想显示我尝试过的具有相同组 id 的所有商店的所有订单,但我得到的是单独的商店组请检查截图你会理解我的问题

对于分组商店,我认为应该同时显示组和商店

先感谢您

// show the last 7 days report
public function getSalesStores()
{
    $weekStart = Carbon::parse('last Sunday')->subdays(7);
    $weekEnd = $weekStart->copy()->adddays(7);

    $allusers = User::getAllUsers();
    $allgroups = storeGroup::getAllGroups();

    $allstores = Storeinfo::select('id', 'store_name', 'site_percent', 'fee_type', 'store_group_id')
        ->whereHas('userorders', function ($query) use ($weekStart, $weekEnd) {
            $query->whereBetween('order_date', [$weekStart, $weekEnd]);
        })
        ->with(['userorders' => function ($query) use ($weekStart, $weekEnd) {
            $query
                ->whereBetween('order_date', [$weekStart, $weekEnd])
                ->whereNotIn('confirmed', ['c', 'a', 'r'])
                ->groupBy('userorders.order_number');
        }])
        ->orderBy('storeinfos.store_name')
        ->get();

        $groupedStores = $allstores->where('store_group_id', '!=', null);


return view('managedashboard.cservicedashboard.sales_orders_bystore.archive', compact('allusers', 'allstores','allgroups','groupedStores'));
}

刀:

   <table class="table table-striped table-bordered table-hover" id="postTable">
    <p class="tabletitle">Sales Report</p>
    <thead>
    <tr>
        <th class="textcenter">Store</th>
        <th class="textcenter">Total</th>
        <th class="textcenter">our fee</th>
        <th class="textcenter">Pay to store</th>
        <th class="textcenter">Order number</th>
        <th class="textcenter">Date</th>
        <th class="textcenter">Store Name</th>
        <th class="textcenter">Customer Name</th>
    </tr>
</thead>
<tbody>   
    @foreach($groupedStores as $key => $store) 

        @php 
            $class = $key % 2 === 0 ? 'even' : 'odd'; 
            $fee=0; 
        @endphp
    <tr class="{{$class}}">
        <td class="storename" rowspan="{{ $store->userorders->count() + 1 }}">               
                  @foreach($allgroups as $allgroup) 
                        @if(($store->store_group_id)==$allgroup->id) 
                            {{$allgroup->name}}
                          @endif   
                   @endforeach
        </td>
    </tr>
     @foreach($store->userorders as $userorder)

            <tr>
                <td class="textcenter">
                    {{number_format($userorder->totalpayment, 2, '.', ',')}}
                </td>

                <td class="textcenter">

                    @if($store->fee_type=='total') 
                        {{number_format(($userorder->totalpayment * ($store->site_percent / 100)), 2, '.', ',')}} 
                        @else 
                        {{number_format(($userorder->order_subtotal * ($store->site_percent / 100)), 2, '.', ',')}}

                    @endif 

                    @php 
                            if($store->fee_type=='total') { 
                            $fee = $fee + number_format(($userorder->totalpayment * ($store->site_percent/100)), 2, '.', ','); 
                                                 } 
                        else { 
                               $fee = $fee + number_format(($userorder->order_subtotal *
                               ($store->site_percent/100)), 2, '.', ','); 
                              } 
                    @endphp
                </td>

                <td class="textcenter">
                    <!-- Pay to Store -->
                    @if($store->fee_type=='total') 
                        {{number_format(($userorder->totalpayment - ($userorder->totalpayment * ($store->site_percent/100))), 2, '.', ',')}} 

                        @else 
                            {{number_format(($userorder->totalpayment -
                            ($userorder->order_subtotal * ($store->site_percent/100))), 2, '.', ',')}} 
                    @endif
                </td>

                <td class="textcenter">
                    {{$userorder->order_number}}
                </td>

                <td class="textcenter">
                    {{date('D d M, Y', strtotime($userorder->order_date))}}
                </td>

                <!--  Store Name  -->
                <td class="textcenter">
                    @foreach($allusers as $alluser) 
                        @if(($userorder->user_id)==$alluser->id) 
                            {{$alluser->fname}} 
                        @endif
                    @endforeach
                </td>

                <!--  Customer Name  -->
                <td class="textcenter">
                    @foreach($allusers as $alluser) 
                        @if(($userorder->user_id)==$alluser->id) 
                            {{$alluser->fname}} 
                        @endif
                    @endforeach
                </td>
            </tr>


            @endforeach <!-- end orders loop -->

    <tr>
        <td class="totalstyle">TOTAL</td>
        <td class="totalstyle">
            {{number_format($store->userorders->sum('totalpayment'), 2, '.', ',')}}
        </td>

        <td class="totalstyle">
            {{$fee}}
        </td>

        <td class="totalstyle">
            {{number_format($store->userorders->sum('totalpayment')-($fee), 2, '.', ',')}}
        </td>
    </tr>

    @endforeach <!-- end main store loop -->

在此处输入图像描述

标签: phplaravel

解决方案


您所需要的只是获取查询数据并相应地重新格式化它。然后,您可以使用 foreach 循环简单地在视图中显示数据。对于模型 StoreGroup、Store 和 Order,您可以执行如下所示的操作。这将为您提供附件“结果”部分中指定格式的数据。

$storeGroupNames = StoreGroup::query()
    ->pluck('name', 'id');
$data = Order::query()
    ->with('store')
    ->get()
    ->groupBy(function (Order $order) {
        return $order->store->store_group_id;
    })
    ->mapWithKeys(function (Collection $orders, $storeGroupId) use ($storeGroupNames) {
        $storeGroupName = $storeGroupNames->get($storeGroupId);

        return [$storeGroupName => $orders];
    });
dd($data);

推荐阅读