F
Filament16mo ago
KarlisJ

Include latest hasMany record in table

I have a Members model with hasMany Payments relatinship. I want to show the latest payment date as a column in the Members table. Is that possible?
6 Replies
bwurtz999
bwurtz99916mo ago
I think it is. You would need to define a new relationship on your Members model, something like latestPayment and then you would chain the filtering info on it something like:
public function latestPayment()
{
return $this->hasMany(Payment::class)->orderBy('created_at', 'desc')->limit(1);
}
public function latestPayment()
{
return $this->hasMany(Payment::class)->orderBy('created_at', 'desc')->limit(1);
}
KarlisJ
KarlisJOP16mo ago
The limit(1) part is a problem. I get only 1 latestPayment per page ...
josef
josef16mo ago
Isnt that what you want? Just the latest payment? In your members table, use TextColum::make('member.latestPayment.date') (or whatever your models and columns are named) if you have a separate payments table, just use the ecisting payments() relation without a limit
public function payments(): HasMany
{
return $this->hasMany(Payment::class):
}

public function latestPayment(): HasOne
{
return $this->hasOne(Payment::class)->latestOfMany():
}
public function payments(): HasMany
{
return $this->hasMany(Payment::class):
}

public function latestPayment(): HasOne
{
return $this->hasOne(Payment::class)->latestOfMany():
}
lazydog
lazydog16mo ago
I used this in my project
public function latestStatus(): HasOne
{
return $this->hasOne(Model::class)->latest();
}
public function latestStatus(): HasOne
{
return $this->hasOne(Model::class)->latest();
}
krekas
krekas16mo ago
latestOfMany() instead of latest()
krekas
krekas16mo ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Want results from more Discord servers?
Add your server