Second Relation not read in Laravel when use with two times

Viewed 36

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

here is the error image enter image description here

thanks before

3 Answers

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

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

i have solve this problem. this problem maybe usually happen to the beginner in laravel like me. so i will answer this question, maybe someone who has the same problem like this, can solve their problem.

in this app before i solve the problem, i call the relation name use table names such jenis_muatan, produk. and it make an error if the relation in model that use different style of function name.

in model, i use JenisMuatan. so, it is different with table name, JenisMuatan to jenis_muatan. then, if i call the name use jenis_muatan, it give error. but if it calls with JenisMuatan, it works perfectly.

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