Display TextColumn based on model function

What is the best apporach for displaying a TextColumn in my table that references a function on my model? I currently have a Model called Ticket which has a function called remaining() which returns the remaining quantity of the ticket as an integer.
Solution:
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.
Jump to solution
4 Replies
Tim van Heugten
Tim van Heugten11mo ago
Is that remaining quantity on a related model? Take a look at https://filamentphp.com/docs/3.x/tables/columns/relationships#aggregating-relationships or maybe use accessor (getRemainingAttribute) on the model.
Markwowz
MarkwowzOP11mo ago
It is not, this is the function on the model: public function remaining(): int { return $this->quantity - $this->bookings()->count(); }
Solution
Tieme
Tieme11mo ago
@Markwowz See : https://laravel.com/docs/10.x/eloquent-serialization#appending-values-to-json
php

protected $appends = ['remaining'];

public function getRemainingAttribute(): int
{
return $this->quantity - $this->bookings()->count();
}
php

protected $appends = ['remaining'];

public function getRemainingAttribute(): int
{
return $this->quantity - $this->bookings()->count();
}
You cant use sortable or searchable on column @Tim van Heugten correct me if i'm wrong
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.
Markwowz
MarkwowzOP11mo ago
This is exactly what I needed! Thank you so much for your help!
Want results from more Discord servers?
Add your server