Support the ongoing development of Laravel.io →
Eloquent Views

Hi

I am having invoices and customers and users how can generete both

Customer Model:

public function invoices()
{
    return $this->hasMany(Invoice::class);
}

Invoice Model:

public function customer()
{
    return $this->belongsTo(Customer::class);
}

When I log in as the first user I can retreive the customer name with:

{{ $invoice->customer->name }}

but when I log in as the second I get:

Undefined property: Illuminate\Database\Eloquent\Collection::$customer

What is happenig here?

Btw I retrieve the invoices in the controller like so:

$invoices = Invoice::where('user_id',Auth::user()->id)->latest()->get();

any help would be greatly appreciated

Last updated 3 years ago.
0

Solved: in the db was a little corrupted There where invoices that did not belong to user customers or something like that. Found out by deleting all user 2 invoices and adding them correctly.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.