Home > Enterprise >  Second Relation not read in Laravel when use with two times
Second Relation not read in Laravel when use with two times

Time:03-12

i have a relationship between SalesTransaction and SalesTransactionDetail, then the SalesTransactionDetailhas Relation with Produk and JenisMuatan Table.

but when i call JenisMuatan in blade.php, it give errors. the error is there is no JenisMuatan->name, but Produk->name is ok, nothing error with Produk.

here is my code :

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

and this is how i implement in view

@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]->produk->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

here is the error image enter image description here

thanks before

CodePudding user response:

From your description you say that "TransactionDetail has Relation with Produk and JenisMuatan Table" but in your code you try to get the relation from SalesTransactionDetail.

Try to change this line :

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

to that:

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

Or if you intented to fetch the SalesTransactionDetail you can fetch 2nd levelrelation ships like this:

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

CodePudding user response:

you are calling two methods that's why it's not loading the second relation.

The proper way to eager load multiple relations is by adding commas, let's take an eye on given below.

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