F
Filament15mo 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
toeknee15mo 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
GushOP15mo 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
toeknee15mo ago
Please provide the pivot table schema?
Gush
GushOP15mo ago
model or migration?
toeknee
toeknee15mo ago
table, or migration if you have a single migration for the pivot I just source dived, do:
$table
->allowDuplicates()
$table
->allowDuplicates()
Instead
Gush
GushOP15mo 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
toeknee15mo ago
Yep that's good, just add on the allowDuplicates to the table definition
Gush
GushOP15mo ago
how so? in the migration? sorry
toeknee
toeknee15mo ago
In the relationship manager on the $table-> declaration
Solution
Gush
Gush15mo 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
GushOP15mo ago
boom solved, thank you sir!
toeknee
toeknee15mo ago
Yep I'll do a PR to the docs
toeknee
toeknee15mo ago
Funny as Dan updated the docs 5 hours ago, so on the next Filament release it'll be deployed I suspect.
Gush
GushOP15mo ago
Nice 💛

Did you find this page helpful?