【发布时间】:2019-08-21 20:36:51
【问题描述】:
我有 3 个关于不同项目的集合,我需要显示并按日期分组,在刀片文件中,我循环显示这些集合的日期,但由于某种原因,无论第一个集合是什么,它都会出现问题,它不应该根据日期而改变,假设我有两个日期(2019-03、2019-01),我最终得到同一个集合的两个不同实例,一个带有数据,一个没有(一个空集合),即使我调用的是同一个变量。
控制器方法:
public function index()
{
//Retrieving the Models.
$invoices_egp = auth()->user()->invoices()->where('paid', 1)->where('currency', 'EGP');
$invoices_usd = auth()->user()->invoices()->where('paid', 1)->where('currency', 'USD');
$orders = \App\Order::where('vendor_id', auth()->user()->id)->where('paid', 1);
//Getting the different dates of these Models.
$egp_invoices_dates = $invoices_egp->get()->map(function ($invoice) {
return Carbon::parse($invoice->created_at)->format('Y-m');
});
$usd_invoices_dates = $invoices_usd->get()->map(function ($invoice) {
return Carbon::parse($invoice->created_at)->format('Y-m');
});
$orders_dates = $orders->get()->map(function ($order) {
return Carbon::parse($order->created_at)->format('Y-m');
});
//Getting the unique dates.
$dates = $orders_dates->merge($usd_invoices_dates)->merge($egp_invoices_dates)->unique();
return view('dashboard.vendor.reports.index', compact('invoices_egp', 'invoices_usd', 'orders', 'dates'));
}
刀片文件的相关部分:
<div class="col-lg-12">
@if ( count( $dates ) )
@foreach($dates as $date)
<div class="card-box">
<div class="table-responsive">
<table class="table table-actions-bar m-b-0">
<thead>
<tr>
<th>
Month
</th>
</tr>
<tr>
<td>
{{ $date }}
</td>
</tr>
</thead>
<tbody>
<tr>
<th colspan="100%">Type</th>
<th>Total</th>
</tr>
<tr>
@if(count($invoices_egp->get()))
<td colspan="100%">Invoice in EGP</td>
<td>{{ $invoices_egp->whereYear('paid_at', \Carbon\Carbon::parse($date)->year)->whereMonth('paid_at', \Carbon\Carbon::parse($date)->month)->sum('total') }}</td>
@endif
</tr>
<tr>
@if(count($invoices_usd->get()))
<td colspan="100%">Invoice in USD</td>
<td>{{ $invoices_usd->whereYear('paid_at', \Carbon\Carbon::parse($date)->year)->whereMonth('paid_at', \Carbon\Carbon::parse($date)->month)->sum('total') * \App\ConversionRate::dollarToEGP() }}</td>
@endif
</tr>
<tr>
@if(count($orders->get()))
<td colspan="100%">Orders</td>
<td>{{ $orders->whereYear('paid_at', \Carbon\Carbon::parse($date)->year)->whereMonth('paid_at', \Carbon\Carbon::parse($date)->month)->sum('total') }}</td>
@endif
</tr>
</tbody>
</table>
</div>
</div>
@endforeach
@else
<div class="card-box">
<h3>No Data</h3>
</div>
@endif
</div>
在这里,当我遍历 $dates 变量时,由于某种原因,$invoices_egp 变量会根据日期发生变化,即使它与它无关,如果我试图转储$invoices_egp (其中有两条记录,日期相同,日期为 2019-01),无论日期如何,我都希望获得这两条记录两次,而不是在第一个循环中获得两条记录($date = 2019-03),然后在第二个循环中 ($date = 2019-01) 我得到一个空集合。
我尝试了不同的东西,我用硬编码数组替换了日期变量并删除了其他日期查询,刀片文件中没有任何变化。
更奇怪的是,如果我用 $invoices_usd 更改 $invoices_egp 的位置,我会正确呈现 $invoices_egp,而错误会发生在 $invoices_usd 变量上,所以无论第一个变量是,它搞砸了。
小更新
我还不知道出了什么问题,但是一旦我注释掉这一行
<td>{{ $invoices_egp->whereYear('paid_at', \Carbon\Carbon::parse($date)->year)->whereMonth('paid_at', \Carbon\Carbon::parse($date)->month)->sum('total') }} EGP</td>
对于循环的每个实例,我都正确地渲染了两次变量,这是应该发生的,我正在评论的这一行不应该对是否应该成功检索集合有任何影响,我希望我正在制作感觉。
如果我在每个循环中转储变量,这就是我在第一个循环中得到的结果
Collection {#522 ▼
#items: array:2 [▼
0 => Invoice {#526 ▶}
1 => Invoice {#519 ▶}
]
}
这就是我在第二个循环中得到的结果(上述行未注释)
Collection {#516 ▼
#items: []
}
相同的变量,循环中的不同结果。
【问题讨论】:
-
首先,如果 'count($invoices_egp->get())',你应该使用 '$invoices_egp->count()'。
-
@LuckySaini 你是对的,谢谢,