Laravel集合在foreach循环内变空

问题描述 投票:0回答:1

我有3个不同项目的集合,我需要显示和按日期分组,在刀片文件中我循环显示这些集合的日期,但出于某种原因,无论第一个集合是什么,它都会发生错误,它不应该' t根据日期改变,假设我有两个日期(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: []
}

相同的变量,循环上的不同结果。

php laravel eloquent
1个回答
0
投票

显然,因为我将一个查询构建器实例传递给视图,它实际上被foreach循环改变了,这是我从未想过会发生的事情,无论如何修复在那时很容易,只需更改代码一点点,从这里开始:

public function index()
{
    $invoices_egp = auth()->user()->invoices()->where(['paid' => 1, 'currency' => 'EGP'])->latest()->get();
    $invoices_usd = auth()->user()->invoices()->where(['paid' => 1, 'currency' => 'USD'])->latest()->get();
    $orders = \App\Order::where('vendor_id', auth()->user()->id)->where('paid', 1)->latest()->get();

    $egp_invoices_dates = $invoices_egp->map(function($invoice) { return Carbon::parse($invoice->paid_at)->format('Y-m'); })->unique();
    $usd_invoices_dates = $invoices_usd->map(function($invoice) { return Carbon::parse($invoice->paid_at)->format('Y-m'); })->unique();
    $orders_dates = $orders->map(function($order) { return Carbon::parse($order->paid_at)->format('Y-m'); })->unique();

    $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'));
}

在视图中,我只需要为所有3个查询更改过滤查询:

<td>{{ $invoices_egp->filter(function($invoice) use ($date) { return $invoice->created_at->year == \Carbon\Carbon::parse($date)->year; })->filter(function($invoice) use ($date) { return $invoice->created_at->month == \Carbon\Carbon::parse($date)->month; })->sum('total') }} EGP</td>
© www.soinside.com 2019 - 2024. All rights reserved.