How to force relation manager to use a specific table column?
I have a DB table
I have 3 models using one common table
First model is
Also corresponding resources
I have created relation manager for all three of them. First for AnimalResource and then for DogResource and CatResource.
I ended up with this:
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
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.
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
Look at the following documentation : https://laravel.com/docs/10.x/eloquent-relationships#one-to-many
you need something like this :
you need something like this :
return $this->hasMany(Comment::class, 'foreign_key', 'animimal_id');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.
