Call to undefined method using Relation Manager Attach
Hi,
I have an Exercise model and each exercise can have some 'related exercises' where the relations are stored in a pivot table.
I'm trying to use a Filament Relation Manager to attach/detach related exercises in a table under the Exercise resource form.
The table populates as it should with the seeded related exercises. And I can detach related exercises too. So it feels like I've got things set up correctly.
But I have a problem attaching new related exercises.
I can click the Attach button from the form and the modal pops up with a searchable select field. But when I try to search, I get the error "Call to undefined method App\Models\Exercise::exercises()"
The Laravel error page actually correctly suggests the solution "Did you mean App\Models\Exercise::relatedExercises() ?" which I do want to use.
But my problem is, I can't figure out where to define the correct method, or even why it's looking for an "exercises()" method when it's not specified anywhere.
In Exercise.php model
In RelatedExercisesRelationManager.php
In ExerciseResource.php
My two database tables look like this:
- exercises
-- id
-- name
- related_exercises
-- exercise_id
-- related_exercise_id
Any help would be very much appreciated.
Solution:Jump to solution
@pboivin You're right, it turns out I did need the inverse relationship.
In Exercise.php model I added:
```
public function exercisesRelatedTo(): BelongsToMany...
5 Replies
Can you get to the error page for this:
"Call to undefined method App\Models\Exercise::exercises()"
and click the SHARE link at the top of the page?
The BelongsToMany looks correct... you could be missing a setting on the Filament side@pboivin - thanks for getting back to me. I do feel like I've missed something so obvious that I just can't see it!
Here's the error page -
Hmm, did you setup an inverse relationship? It's a bit weird because the model relates back to itself but I think you might still need the inverse relationship.
Solution
@pboivin You're right, it turns out I did need the inverse relationship.
In Exercise.php model I added:
And in RelatedExercisesRelationManager.php I added
I'm not entirely sure why this is needed, but it works! I can now search and attach in the modal as expected.
Thank you for your help.
Awesome, that's good to know! I'm sure it will be useful again in the future.