F
Filamentβ€’13mo ago
Gush

Allowing to attach duplicate records

in a belongtomany relationship how do i change the default behavior of not allowing to attach one record twice or more, i already read the docs but its limited and i really dont understand how
Solution:
``` public function table(Table $table): Table { return $table ->recordTitle(fn (Equipment $record): string => "{$record->name}\n ({$record->equipmentbrands->name}, {$record->equipmentmodels->name}, {$record->equipmentcategories->name})")...
Jump to solution
14 Replies
toeknee
toekneeβ€’13mo ago
So it says right here: https://filamentphp.com/docs/3.x/panels/resources/relation-managers#handling-duplicates Just add the allowsDuplicates as per the docs to the relationship manager code, providing you have set a primary ID on the relationshiped table
Gush
GushOPβ€’13mo ago
i did the following:
class EquipmentRelationManager extends RelationManager
{
protected static string $relationship = 'equipment';

protected static ?string $title = '';

protected bool $allowsDuplicates = true;

...
class EquipmentRelationManager extends RelationManager
{
protected static string $relationship = 'equipment';

protected static ?string $title = '';

protected bool $allowsDuplicates = true;

...
didnt work and i think i do have primary ID in the pivot table
toeknee
toekneeβ€’13mo ago
Please provide the pivot table schema?
Gush
GushOPβ€’13mo ago
model or migration?
toeknee
toekneeβ€’13mo ago
table, or migration if you have a single migration for the pivot I just source dived, do:
$table
->allowDuplicates()
$table
->allowDuplicates()
Instead
Gush
GushOPβ€’13mo ago
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('construction_has_equipment', function (Blueprint $table) {
$table->id();
$table->foreignId('construction_id')->constrained()->onDelete('cascade');
$table->foreignId('equipment_id')->constrained()->onDelete('cascade');
$table->string('serial_number')->unique()->nullable();
$table->softDeletes();
$table->norlogicFields();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('construction_equipment');
}
};
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('construction_has_equipment', function (Blueprint $table) {
$table->id();
$table->foreignId('construction_id')->constrained()->onDelete('cascade');
$table->foreignId('equipment_id')->constrained()->onDelete('cascade');
$table->string('serial_number')->unique()->nullable();
$table->softDeletes();
$table->norlogicFields();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('construction_equipment');
}
};
toeknee
toekneeβ€’13mo ago
Yep that's good, just add on the allowDuplicates to the table definition
Gush
GushOPβ€’13mo ago
how so? in the migration? sorry
toeknee
toekneeβ€’13mo ago
In the relationship manager on the $table-> declaration
Solution
Gush
Gushβ€’13mo ago
public function table(Table $table): Table
{
return $table
->recordTitle(fn (Equipment $record): string => "{$record->name}\n ({$record->equipmentbrands->name}, {$record->equipmentmodels->name}, {$record->equipmentcategories->name})")
->allowDuplicates() //here
->columns([
public function table(Table $table): Table
{
return $table
->recordTitle(fn (Equipment $record): string => "{$record->name}\n ({$record->equipmentbrands->name}, {$record->equipmentmodels->name}, {$record->equipmentcategories->name})")
->allowDuplicates() //here
->columns([
Gush
GushOPβ€’13mo ago
boom solved, thank you sir!
toeknee
toekneeβ€’13mo ago
Yep I'll do a PR to the docs
toeknee
toekneeβ€’13mo ago
Funny as Dan updated the docs 5 hours ago, so on the next Filament release it'll be deployed I suspect.
Gush
GushOPβ€’13mo ago
Nice πŸ’›
Want results from more Discord servers?
Add your server