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

Did you find this page helpful?