两次使用时未在 Laravel 中读取第二个关系

Second Relation not read in Laravel when use with two times

我在 SalesTransaction 和 SalesTransactionDetail 之间有关系, 然后 SalesTransactionDetail 与 Produk 和 JenisMuatan 有关系 Table。

但是当我在 blade.php 中调用 JenisMuatan 时,它会出错。错误是没有 JenisMuatan->name,但是 Produk->name 没问题,Produk 没有错误。

这是我的代码:

$sales = SalesTransaction::with('Customer')->with('User')->latest()->get();
        foreach ($sales as $item) {
            $item['detail'] = SalesTransactionDetail::where('sales_transaction_id', $item->id)->with('Produk')->with('JenisMuatan')->get();
        }

这就是我在视图中的实现方式

@for ($i = 0; $i < $sales->count(); $i++)
            @for ($j = 0; $j < $sales[$i]->detail->count(); $j++)
                <tr>
                    @if ($sales[$i]->detail->count() > 1)
                        @if ($j == 0)
                            <td style="text-align: center; height: 20;"
                                rowspan="{{ $j == 0 ? $sales[$i]->detail->count() : 1 }}">
                                {{ $j > 0 ? '' : $i + 1 }}</td>
                            <td style="text-align: left; height: 20;"
                                rowspan="{{ $j == 0 ? $sales[$i]->detail->count() : 1 }}">
                                {{ $j > 0 ? '' : "'" . $sales[$i]->invoice_number }}</td>
                            <td style="text-align: left; height: 20;"
                                rowspan="{{ $j == 0 ? $sales[$i]->detail->count() : 1 }}">
                                {{ ($j > 0 ? '' : $sales[$i]->customer_id == null) ? 'Cash' : 'Deposit : ' . $sales[$i]->customer->name }}
                            </td>
                            <td style="text-align: right; height: 20;"
                                rowspan="{{ $j == 0 ? $sales[$i]->detail->count() : 1 }}">
                                {{ $j > 0 ? '' : $sales[$i]->total_harga }}</td>
                        @endif
                    @endif
                    @if ($sales[$i]->detail->count() <= 1)
                        <td style="text-align: center; height: 20;">{{ $i + 1 }}</td>
                        <td style="text-align: left; height: 20;">{{ "'" . $sales[$i]->invoice_number }}</td>
                        <td style="text-align: left; height: 20;">
                            {{ $sales[$i]->customer_id == null ? 'Cash' : 'Deposit : ' . $sales[$i]->customer->name }}
                        </td>

                        <td style="text-align: right; height: 20;"
                            rowspan="{{ $j == 0 ? $sales[$i]->detail->count() : 1 }}">
                            {{ $j > 0 ? '' :  $sales[$i]->total_harga }}</td>
                    @endif
                    <td style="text-align: left; height: 20;">
                        {{ $sales[$i]->detail[$j]->produk->name }}</td>
                    <td style="text-align: left; height: 20;">
                        {{ $sales[$i]->detail[$j]->jenis_muatan->name }}</td>
                    <td style="text-align: right; height: 20;">
                        {{ $sales[$i]->detail[$j]->total_harga }}</td>

                        @if ($sales[$i]->detail->count() > 1)
                        @if ($j == 0)
                            <td style="text-align: left; height: 20;"
                                rowspan="{{ $j == 0 ? $sales[$i]->detail->count() : 1 }}">
                                {{ $j > 0 ? '' : "'" . $sales[$i]->created_at }}</td>
                        @endif
                    @endif
                    @if ($sales[$i]->detail->count() <= 1)
                        <td style="text-align: left; height: 20;"
                            rowspan="{{ $j == 0 ? $sales[$i]->detail->count() : 1 }}">
                            {{ $j > 0 ? '' : "'" . $sales[$i]->created_at }}</td>
                    @endif
                </tr>
            @endfor
        @endfor

这是错误图片

之前感谢

根据您的描述,您说“TransactionDetail 与 Produk 和 JenisMuatan Table 有关系”,但在您的代码中,您尝试从 SalesTransactionDetail 获取关系。

尝试更改此行:

 $item['detail'] = SalesTransactionDetail::where('sales_transaction_id', $item->id)->with('Produk')->with('JenisMuatan')->get();

到那个:

 $item['detail'] = TransactionDetail::where('sales_transaction_id', $item->id)->with('Produk')->with('JenisMuatan')->get();

或者,如果您打算获取 SalesTransactionDetail,您可以像这样获取二级关系船:

$item['detail'] = SalesTransactionDetail::where('sales_transaction_id', $item->id)->with('TransactionDetail.Produk')->with('TransactionDetail.JenisMuatan')->get();

您正在调用两个方法,这就是它不加载第二个关系的原因。

预加载多个关系的正确方法是添加逗号,让我们看看下面给出的。

$sales = SalesTransaction::with('Customer', 'User')->latest()->get();

我已经解决了这个问题。这个问题可能经常发生在像我这样 laravel 的初学者身上。 所以我会回答这个问题,也许有同样问题的人可以解决他们的问题。

在我解决问题之前,在这个应用程序中,我调用关系名称使用 table 名称,例如 jenis_muatan,produk。如果模型中的关系使用不同样式的函数名称,则会出错。

在模型中,我使用 JenisMuatan。所以,table name,JenisMuatan 和 jenis_muatan 是不同的。然后,如果我调用名称 use jenis_muatan,它会出错。但是如果它调用 JenisMuatan,它会完美地工作。

jenis_muatan->name // error
JenisMuatan->name //work