Summarize Table RelationManager

I am attempting to use the summarize feature from Filament on a table that exists in RelationManager. However, I encounter an error:
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id'
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id'
When I remove $table->id() in create_package_product_table.php, the error changes to:
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created_at'
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created_at'
If I remove $table->timestamps() in create_package_product_table.php, the summarize feature can be used. How can I use summarize on a table in RelationManager without having to remove $table->id() and $table->timestamps()?
2 Replies
Kaesa Lyrih
Kaesa Lyrih8mo ago
Code: - Pivot table: create_package_product_table.php
public function up(): void
{
Schema::create('package_product', function (Blueprint $table) {
$table->id();
$table->foreignId('package_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('package_product', function (Blueprint $table) {
$table->id();
$table->foreignId('package_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
- Model Package.php
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class, 'package_product')->withPivot('id')->withTimestamps();
}
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class, 'package_product')->withPivot('id')->withTimestamps();
}
- Model Product.php
public function packages(): BelongsToMany
{
return $this->belongsToMany(Package::class, 'package_product')->withPivot('id')->withTimestamps();
}
public function packages(): BelongsToMany
{
return $this->belongsToMany(Package::class, 'package_product')->withPivot('id')->withTimestamps();
}
- ProductResource.php
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name'),
TextColumn::make('price')->summarize(Sum::make()),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name'),
TextColumn::make('price')->summarize(Sum::make()),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
- ProductRelationManager.php in PackageResource.php
public function table(Table $table): Table
{
return ProductResource::table($table)
->headerActions([
Tables\Actions\AttachAction::make()->preloadRecordSelect(),
])
->actions([
Tables\Actions\DetachAction::make(),
])
->groupedBulkActions([
Tables\Actions\DetachBulkAction::make(),
]);
}
public function table(Table $table): Table
{
return ProductResource::table($table)
->headerActions([
Tables\Actions\AttachAction::make()->preloadRecordSelect(),
])
->actions([
Tables\Actions\DetachAction::make(),
])
->groupedBulkActions([
Tables\Actions\DetachBulkAction::make(),
]);
}
Mark Chaney
Mark Chaney4mo ago
@Kaesa Lyrih did you ever figure this out? looks like you just have to set your own logic on using() to do it. Like so:
->summarize(Tables\Columns\Summarizers\Summarizer::make()
->label('Total Amount')
->using(function ($query) {
$amount = 0;
$query->get()->each(function ($record) use (&$amount) {
$item = UnitUtilityAllowanceItem::find($record->id);
$amount += $item->amount();
});

return currency_format($amount);
})),
->summarize(Tables\Columns\Summarizers\Summarizer::make()
->label('Total Amount')
->using(function ($query) {
$amount = 0;
$query->get()->each(function ($record) use (&$amount) {
$item = UnitUtilityAllowanceItem::find($record->id);
$amount += $item->amount();
});

return currency_format($amount);
})),