我正在为项目中的库存做报告。现在,我想根据日期之间显示items
。我想通过将created_at
分组如下所示在视图中显示项目列表,
在控制器方法中,我写了-
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>
答案 0 :(得分:1)
在groupBy
方法之后使用laravel 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>