groupby('created_at')在laravel-eloquent中使用子查询

mhd8tkvw  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(403)

我正在为我的项目做一个库存报告。现在,我想展示一下 items 基于日期之间。我想通过分组 created_at 有点像贝娄-

我的数据库表结构是-

在controller方法中,我写道-

public function inventorySubmit(Request $request)
{
    $itemId     = $request->item;
    $startDate  = Carbon::parse($request->start_date)->format('Y-m-d');
    $endDate    = Carbon::parse($request->end_date)->format('Y-m-d');
    $reportType = $request->report_type;

    if ($itemId) {
        $items = Item::where('id', $itemId)->whereBetween('created_at', [ $startDate, $endDate ])->where('status', 1)->get();
    }else{
        $items = Item::whereBetween('created_at', [ $startDate, $endDate ])->where('status', 1)->get();
    }
    return view('report.inventory.searched-result', compact('reportType', 'startDate', 'endDate', 'items'));
}

在刀刃上,我表现出-

<table class="table text-center" style="text-align: left; width: 100%;">
    <tr>
        <td style="font-weight: bold;">#</td>
        <td style="font-weight: bold;">Date</td>
        <td style="font-weight: bold;">Opening Balance</td>
    </tr>
    @foreach($items as $item)
    <tr>
        <td>{{ $loop->index + 1 }}</td>
        <td>{{ date('jS F, Y', strtotime($item->created_at)) }}</td>
        <td>
            <span>{{ $item->name }}</span> - <span>{{ $item->opening_balance }} <br></span>
        </td>
    </tr>
    @endforeach

</table>
rdrgkggo

rdrgkggo1#

使用laravel groupBy 之后 get 像这样的方法
控制器

public function inventorySubmit(Request $request)
{
    $itemId     = $request->item;
    $startDate  = Carbon::parse($request->start_date)->format('Y-m-d');
    $endDate    = Carbon::parse($request->end_date)->format('Y-m-d');
    $reportType = $request->report_type;

    if ($itemId) {
        $items = Item::select('*', DB::raw('Date(created_at) as date'))
                      ->where('id', $itemId)->whereBetween('created_at', [ $startDate, $endDate ])
                      ->where('status', 1)
                      ->get()
                      ->groupBy('date');
    }else{
        $items = Item::select('*', DB::raw('Date(created_at) as date'))
                      ->whereBetween('created_at', [ $startDate, $endDate ])
                      ->where('status', 1)
                      ->get()
                      ->groupBy('date');
    }
    return view('report.inventory.searched-result', compact('reportType', 'startDate', 'endDate', 'items'));
}

在视图中

<table class="table text-center" style="text-align: left; width: 100%;">
    <tr>
        <td style="font-weight: bold;">#</td>
        <td style="font-weight: bold;">Date</td>
        <td style="font-weight: bold;">Opening Balance</td>
    </tr>
    @foreach($items as $groupitem)
    <tr>
        <td>{{ $loop->index + 1 }}</td>
        <td>{{ date('jS F, Y', strtotime($groupitem->first()->created_at)) }}</td>
        <td>
            @foreach($groupitem as $item)
               <span>{{ $item->name }}</span> - <span>{{ $item->opening_balance }} <br></span>
            @endforeach
        </td>
    </tr>
    @endforeach

</table>

相关问题