Lewati ke konten utama

2023-11-14

php artisan migrate:rollback && php artisan migrate && php artisan db:seed --class=AcademicYearSeeder && php artisan db:seed --class=SchoolSeeder && php artisan db:seed --class=ProductSeeder

Summarize RelationManager

Saya ingin menggunakan summarize filament pada table yang ada pada RelationManager, akan tetapi saya mendapatkan error

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id'

Jika saya menghapus $table-id() pada create_package_product_table.php maka error berubah menjadi:

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created_at'

Jika saya menghapus $table->timestamps() pada create_package_product_table.php, summarize dapat digunakan.

  • 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();
});
}
  • Model Package.php
 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();
}
  • ProductResource.php
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name')->label('Nama'),
TextColumn::make('price')->label('Harga')->currency('IDR', true)->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(),
]);
}