Trouble with Relation Manager for Many-to-Many Relationship

Hello everyone, I'm experiencing issues with managing the Filament relation manager. Currently, I have two Laravel models related with a many-to-many relationship. I've followed the steps in the documentation, but I can't understand why I'm not getting any results. The manager table is not displayed in the create or edit views of the resource, and all this happens without triggering any errors. I'll paste the formatted code that's needed for reference. I'm pretty confident the issue lies on the Filament side, as I've tested the relationship with Laravel Tinker, and it works correctly. I'm not getting any results in the edit/create views of the resource. Can anyone help me figure out what's going wrong? KiidPdfPattern Model:
class KiidPdfPattern extends Model
{
use HasFactory;

public $timestamps = false;

protected $table = 'kiid_dati_tipo';
protected $primaryKey = 'lunivid';
protected $guarded = [];

public function kiidMailPdfs(): BelongsToMany
{
return $this->belongsToMany(KiidMailPdf::class, 'kiid_mail_pdf_kiid_dati_tipo', 'kiid_dati_tipo_lunivid', 'kiid_mail_pdf_lunivid');
}
}
class KiidPdfPattern extends Model
{
use HasFactory;

public $timestamps = false;

protected $table = 'kiid_dati_tipo';
protected $primaryKey = 'lunivid';
protected $guarded = [];

public function kiidMailPdfs(): BelongsToMany
{
return $this->belongsToMany(KiidMailPdf::class, 'kiid_mail_pdf_kiid_dati_tipo', 'kiid_dati_tipo_lunivid', 'kiid_mail_pdf_lunivid');
}
}
KiidMailPdf Model:
class KiidMailPdf extends Model
{
use HasFactory;

protected $table = 'kiid_mail_pdf';
//protected $connection = 'palliauto';

public $timestamps = false;

protected $primaryKey = 'lunivid';
protected $guarded = [];

public function kiidPdfPatterns(): BelongsToMany
{
return $this->belongsToMany(KiidPdfPattern::class, 'kiid_mail_pdf_kiid_dati_tipo', 'kiid_mail_pdf_lunivid', 'kiid_dati_tipo_lunivid');
}
}
class KiidMailPdf extends Model
{
use HasFactory;

protected $table = 'kiid_mail_pdf';
//protected $connection = 'palliauto';

public $timestamps = false;

protected $primaryKey = 'lunivid';
protected $guarded = [];

public function kiidPdfPatterns(): BelongsToMany
{
return $this->belongsToMany(KiidPdfPattern::class, 'kiid_mail_pdf_kiid_dati_tipo', 'kiid_mail_pdf_lunivid', 'kiid_dati_tipo_lunivid');
}
}
...other code in the comments! Thank you all guys! 🙂
2 Replies
SirAlyon
SirAlyon5mo ago
Pivot Migration:
public function up(): void
{
Schema::create('kiid_mail_pdf_kiid_dati_tipo', function (Blueprint $table) {
$table->unsignedBigInteger('kiid_mail_pdf_lunivid');
$table->foreign('kiid_mail_pdf_lunivid')->references('lunivid')->on('kiid_mail_pdf');

$table->unsignedBigInteger('kiid_dati_tipo_lunivid')->nullable();
$table->foreign('kiid_dati_tipo_lunivid')->references('lunivid')->on('kiid_dati_tipo');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::drop('kiid_mail_pdf_kiid_dati_tipo');
}
public function up(): void
{
Schema::create('kiid_mail_pdf_kiid_dati_tipo', function (Blueprint $table) {
$table->unsignedBigInteger('kiid_mail_pdf_lunivid');
$table->foreign('kiid_mail_pdf_lunivid')->references('lunivid')->on('kiid_mail_pdf');

$table->unsignedBigInteger('kiid_dati_tipo_lunivid')->nullable();
$table->foreign('kiid_dati_tipo_lunivid')->references('lunivid')->on('kiid_dati_tipo');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::drop('kiid_mail_pdf_kiid_dati_tipo');
}
KiidResource:
public static function getRelations(): array

{
return [
RelationManagers\KiidPdfPatternRelationManager::class
];
}

public static function getPages(): array
{
return [
'index' => Pages\ManageKiidResource::route('/'),
/* 'index' => Pages\ListKiidResource::route('/'),
'create' => Pages\CreateKiidResource::route('/create'),
'edit' => Pages\CreateKiidResource::route('/edit'), */
];
}
public static function getRelations(): array

{
return [
RelationManagers\KiidPdfPatternRelationManager::class
];
}

public static function getPages(): array
{
return [
'index' => Pages\ManageKiidResource::route('/'),
/* 'index' => Pages\ListKiidResource::route('/'),
'create' => Pages\CreateKiidResource::route('/create'),
'edit' => Pages\CreateKiidResource::route('/edit'), */
];
}
KiidPdfPatternRelationManager (under Resources\KiidResource\RelationManager)
class KiidPdfPatternRelationManager extends RelationManager
{
protected static string $relationship = 'kiidPdfPatterns';


public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('cnomedato')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('cnomedato')
->columns([
Tables\Columns\TextColumn::make('cnomedato'),
])
->inverseRelationship('kiidMailPdf')
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
class KiidPdfPatternRelationManager extends RelationManager
{
protected static string $relationship = 'kiidPdfPatterns';


public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('cnomedato')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('cnomedato')
->columns([
Tables\Columns\TextColumn::make('cnomedato'),
])
->inverseRelationship('kiidMailPdf')
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Pump 🙂
Melchior
Melchior5mo ago
Checked the global scopes? Checked policies? You can try to get the query on the table and dd-ing it to see if it matches your expectations