【问题标题】:Laravel collection turns empty inside foreach loopsLaravel 集合在 foreach 循环中变为空
【发布时间】: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 你是对的,谢谢,

标签: php laravel eloquent


【解决方案1】:

显然,由于我将查询构建器实例传递给视图,实际上被 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>

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 2018-06-20
    • 2018-05-13
    • 2018-12-25
    • 2013-10-09
    • 2018-10-09
    • 2021-09-07
    • 1970-01-01
    • 2018-08-05
    相关资源
    最近更新 更多