Video Version of the Lesson
[Only for premium members]
[Only for premium members]
[Only for premium members]
Filament also offers convenient ways to group table data by columns and calculate the aggregated data like "sum" or "average" to show at the bottom.
To demonstrate that, I've created a new Model for Orders:
php artisan make:model Order -m
Migration:
Schema::create('orders', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained(); $table->foreignId('product_id')->constrained(); $table->integer('price'); $table->timestamps();});
app/Models/Order.php:
class Order extends Model{ protected $fillable = ['user_id', 'product_id', 'price']; public function user(): BelongsTo { return $this->belongsTo(User::class); } public function product(): BelongsTo { return $this->belongsTo(Product::class); }}
And added some data for the orders:
Then, we can generate a new Filament Resource to show the table of Orders:
php artisan make:filament-resource Order
The table has these values:
app/Filament/Resources/OrderResource.php:
class OrderResource extends Resource{ // ... public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('created_at') ->dateTime(), Tables\Columns\TextColumn::make('product.name'), Tables\Columns\TextColumn::make('user.name'), Tables\Columns\TextColumn::make('price') ->money('usd') ->getStateUsing(function (Order $record): float { return $record->price / 100; }) ]) ->defaultSort('created_at', 'desc') // ... other methods with default values }}
Here's how it looks now:
What if you want to divide this table by some condition? Let's try to group by product. Then, you just...