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
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
Please or to participate in this conversation.