拉拉维尔每周分组由雄辩的收集错误

  • 本文关键字:错误 每周 laravel eloquent
  • 更新时间 :
  • 英文 :


我有订单表,以及每个用户的每周付款表。 我想向用户展示他将根据周的总付款数。 我已经创建了查询,但在刀片端,当我尝试通过 eloquent 访问其他表时,它不会在此集合中退出。

如果我不使用按功能分组,它可以工作。但是通过按功能分组,它确实

$earnings = user()->orders()->where('payment_status',0)- 
>where('status',1)->get()->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('W');
});

在刀片侧,我的代码如下;

@foreach($earnings as $earning)
<tr>
<td>{{$earning->user->name}}</td>
<td>{{$earning->order->commission}}</td>
<td>{{$earning->user->bank}}</td>
<td>{{$earning->user->iban}}</td>
<td>{{$earning->user->phone}}</td>
@endforeach

get()应该是最后一个:

$earnings = user()->orders()
->where('payment_status',0)
->where('status',1)
->groupBy('created_at')
->get();

如何使用DATE_FORMAT函数:

$earnings = user()->orders()
->where('payment_status',0)
->where('status',1)
->groupBy(DB::raw("DATE_FORMAT(created_at, '%W')"))
->get();

假设你的表是orders的,你可以这样做:

$earnings = user()->orders()->select('orders.*', DB::raw("DATE_FORMAT(created_at, '%W') as week"))
->where('payment_status',0)
->where('status',1)
->get()->groupBy('week');

Laravel 集合groupBy()函数返回分组集合的集合。

$earningGroups = user()->orders()
->where('payment_status',0)
->where('status',1)
->get()
->groupBy(function($earning) {
return Carbon::parse($earning->created_at)->format('W');
});

现在这是收入组的集合,而不是收入。

@foreach($earningGroups as $earcningGroupName => $earcningGroup)
// So here you have a group or earnings
// I don't know why did you want to group.
// For an example I will print the group name as a table row  here.
// But do whatever you want.
<tr>
<td>{{ $earningGroupName }}</td>
</tr>
// So if you want to loop through earnings, you have to loop again.
@foreach($earningGroup as $earning)
<tr>
<td>{{$earning->user->name}}</td>
<td>{{$earning->order->commission}}</td>
<td>{{$earning->user->bank}}</td>
<td>{{$earning->user->iban}}</td>
<td>{{$earning->user->phone}}</td>
</tr>
@endforeach
@endforeach

相关内容

  • 没有找到相关文章

最新更新