F
Filamentβ€’8mo 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β€’8mo 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
Gushβ€’8mo 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β€’8mo ago
Please provide the pivot table schema?
Gush
Gushβ€’8mo ago
model or migration?
toeknee
toekneeβ€’8mo ago
table, or migration if you have a single migration for the pivot I just source dived, do:
$table
->allowDuplicates()
$table
->allowDuplicates()
Instead
Gush
Gushβ€’8mo 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β€’8mo ago
Yep that's good, just add on the allowDuplicates to the table definition
Gush
Gushβ€’8mo ago
how so? in the migration? sorry
toeknee
toekneeβ€’8mo ago
In the relationship manager on the $table-> declaration
Solution
Gush
Gushβ€’8mo 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
Gushβ€’8mo ago
boom solved, thank you sir!
toeknee
toekneeβ€’8mo ago
Yep I'll do a PR to the docs
toeknee
toekneeβ€’8mo ago
Funny as Dan updated the docs 5 hours ago, so on the next Filament release it'll be deployed I suspect.
Gush
Gushβ€’8mo ago
Nice πŸ’›
Want results from more Discord servers?
Add your server
More Posts