F
Filament•9mo ago
Lietze

Relationmanager for other items from same user

I have a payment and user model. On the payment page i would like to show all the other payments from that same user, is this possible? And if so, what would be the simplest way to achieve this? so basically a relationmanager for payment.user.payments
5 Replies
krekas
krekas•9mo ago
maybe it is possible to use hasManyThrought()?
ChesterS
ChesterS•9mo ago
Can you add some more info? Is the payment page the payment form or the payment view?
Lietze
Lietze•9mo ago
On the PaymentResource i want to get a list below the view with all the other payments of this user something like this:
public static function getRelations(): array
{
return [
UserPaymentsRelationManager::class,
];
}
public static function getRelations(): array
{
return [
UserPaymentsRelationManager::class,
];
}
but i'm not sure how i would get these other payments, since this doesn't work of course:
// on the UserPaymentsRelationManager.php
protected static string $relationship = 'user.payments';
// on the UserPaymentsRelationManager.php
protected static string $relationship = 'user.payments';
would i need to make a new method in my model? or is it possible with just filament code? After creating a new method i've made it work using a hasMany relation 🙂 Update: HasManyThrough*
// in the Payment model:
public function paymentsByUser(): HasManyThrough
{
return $this->hasManyThrough(Payment::class, User::class, 'id', 'user_id', 'user_id', 'id');
}

// in the relation manager:
protected static string $relationship = 'paymentsByUser';
// in the Payment model:
public function paymentsByUser(): HasManyThrough
{
return $this->hasManyThrough(Payment::class, User::class, 'id', 'user_id', 'user_id', 'id');
}

// in the relation manager:
protected static string $relationship = 'paymentsByUser';
ConnorHowell
ConnorHowell•9mo ago
That works but a cleaner more “eloquent” way would have been to use a HasManyThrough relation: https://laravel.com/docs/10.x/eloquent-relationships#has-many-through
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.
Lietze
Lietze•9mo ago
yeah, you're correct. Noticed something was wrong and when i changed it to a hasManyThrough everything worked perfectly!