How to force relation manager to use a specific table column?
I have a DB table
notes
as a relation manager (to store notes about various animals) and there is animal_id
column (not a primary key, that is traditionally id
as usual, just an arbitrary integer identifier).
I have 3 models using one common table animals
.
First model is Animal
(the original one) and additional two I created later: Dog
and Cat
. They all use the same table animals
.
Also corresponding resources AnimalResource
, DogResource
and CatResource
.
I have created relation manager for all three of them. First for AnimalResource and then for DogResource and CatResource.
I ended up with this:
App\Filament\Resources\AnimalResource\RelationManagers\NotesRelationManager.php
for notes for AnimalResource.
App\Filament\Resources\DogResource\RelationManagers\NotesRelationManager.php
for notes for DogResource.
App\Filament\Resources\CatResource\RelationManagers\NotesRelationManager.php
for notes for CatResource.
Showing lists of notes under the form of AnimalResource works.
Showing the lists of notes under the form of DogResource and CatResource doesn't work.
When I scroll down on the edit page of DogResource or CatResource I will get this error message:
for dogs and:
for cats.
Filament should use notes.animal_id
instead of notes.dog_id
or notes.cat_id
to get the results from the notes
table based on the value in the animal_id
column.
How to force Filament to use animal instead of dog or cat for building the query for the relation manager?
Is there any way to set the query id name to by used across the queries in relation manager? Currently it is grabing it automatically from the namespace or filename it seems.Solution:Jump to solution
Look at the following documentation : https://laravel.com/docs/10.x/eloquent-relationships#one-to-many
you need something like this :
return $this->hasMany(Comment::class, 'foreign_key', 'animimal_id');
...Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
8 Replies
EDIT:
Here is the query call that Laravel errors out:
Filament should use
notes.animal_id
instead of notes.dog_id
or notes.cat_id
to get the results from the notes
table based on the value in the animal_id
column. But it is instead trying to get value from dog_id
, but such column doesn't exist. It's just the name of the model (Dog
) and resource (DogResource
), but the table and column is animal
and animal_id
.
Any idea how can this be solved?
Is this the problem? https://laravel.com/docs/10.x/eloquent#primary-keys
or rather this https://laravel.com/docs/10.x/eloquent#retrieving-single-models
It's probably not possible to use two resources with one model and not being stuck when using relation manager in both. Despite many people here told me that's the way to go 😭
Is it possible to tell Filament to use a specific column when using Relation Manager and not what he derives from the namespace?Share your relation model code please
Model or RelationManager file?
model
ok
a econd
and the notes?
Solution
Look at the following documentation : https://laravel.com/docs/10.x/eloquent-relationships#one-to-many
you need something like this :
return $this->hasMany(Comment::class, 'foreign_key', 'animimal_id');
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Yeah, it will probably be the lack of defining foreign key eithr for belongs to or one to many.
Thanks for pointing me in the right direction, at least I hope so 😉
Yes, solved!
hasMany in Dog.php (and Cat.php) model file should look like this:
the foreign key animal_id as a second paramter fixed it.
without it it would use dog_id or cat_id.