Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

caden1337's avatar

Displaying model relationship in blade not working

Hello, I have 3 tables, one of invoices one with coletes and one with incasaris.

I have the following code:

    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Mail;
    Use Illuminate\Support\Facades\Storage;
    Use App\Models\Incasari;
    Use App\Models\colete;


    class Invoice extends Model
    {
    use HasFactory;

    public function incasari(){
    return $this->hasMany(Incasari::class,'i_plati_id','id') 
     ->selectRaw('incasaris.i_plati_id,SUM(incasaris.i_totaleuro) as totalincasat') 
     ->groupBy('i_plati_id');
    }



    public function colete(){
    return $this->hasMany(colete::class,'invoice_id','id') 
    ->selectRaw('coletes.invoice_id,SUM(coletes.totaleuro) as totaleuro') 
    ->groupBy('invoice_id');
    }


     }

In my controller the following function:

    $invoices = Invoice::with(['colete','incasari'])->get();

returns me the data I want when I use DB($invoices)

However, when I want to access the relation in my blade view as:

    @foreach ($invoices as $invoice )
                                                 
                                                                                             
    <tr>
    <td> {{$invoice->id}} </td>
    <td> {{$invoice->serieId}} </td>
    <td> {{$invoice->colete->invoice_id}}  </td>
    </tr>
    @endforeach

It returns: Property [invoice_id] does not exist on this collection instance.

But, If I use :

    @foreach ($invoices as $invoice )
                                                 
                                                                                             
                      <tr>
                         <td> {{$invoice->id}} </td>
                         <td> {{$invoice->serieId}} </td>
                         <td> {{$invoice->colete}}  </td>
                            </tr>
                            @endforeach

It returns me the data as:

    [{"invoice_id":2008,"totaleuro":"3000.00"}]

I am pretty sure this is a syntax error but because I am new to Eloquent I can't really figure it out where

0 likes
3 replies
Sinnbeck's avatar

You told it that it has many, but you try to use it as if you have just 1

@foreach ($invoice->colete as $col)
 <td> {{$col->invoice_id}}  </td>
@endforeach
Tray2's avatar

You really should use only English when you name things, and that makes it easier to follow the Laravel naming conventions, thus giving you a lot of functionality for free.

The most important reason for using English in your code is that it makes it so much easier for others here to help you when you get stuck on a problem. I would estimate 90% of the time spent trying to help, is trying to understand what a variable/table/method is refering to or is expected to do.

Example.

$ordrar =  hamtaOrdrarFranDatabasen()

// Or

$orders = getOrdersFromDB()

Please or to participate in this conversation.