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
public function relatedExercises(): BelongsToMany
{
return $this->belongsToMany(Exercise::class, 'related_exercises', 'exercise_id', 'related_exercise_id');
}
public function relatedExercises(): BelongsToMany
{
return $this->belongsToMany(Exercise::class, 'related_exercises', 'exercise_id', 'related_exercise_id');
}
In RelatedExercisesRelationManager.php
protected static string $relationship = 'relatedExercises';
protected static string $relationship = 'relatedExercises';
In ExerciseResource.php
public static function getRelations(): array
{
return [
RelationManagers\RelatedExercisesRelationManager::class,
];
}
public static function getRelations(): array
{
return [
RelationManagers\RelatedExercisesRelationManager::class,
];
}
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:
@pboivin You're right, it turns out I did need the inverse relationship. In Exercise.php model I added: ``` public function exercisesRelatedTo(): BelongsToMany...
Jump to solution
5 Replies
Patrick Boivin
Patrick Boivin17mo ago
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
danw0921
danw0921OP17mo ago
@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 -
Patrick Boivin
Patrick Boivin17mo ago
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
danw0921
danw092117mo ago
@pboivin You're right, it turns out I did need the inverse relationship. In Exercise.php model I added:
public function exercisesRelatedTo(): BelongsToMany
{
return $this->belongsToMany(Exercise::class, 'related_exercises', 'related_exercise_id', 'exercise_id');
}
public function exercisesRelatedTo(): BelongsToMany
{
return $this->belongsToMany(Exercise::class, 'related_exercises', 'related_exercise_id', 'exercise_id');
}
And in RelatedExercisesRelationManager.php I added
public function table(Table $table): Table
{
return $table
...
->inverseRelationship('exercisesRelatedTo');
}
public function table(Table $table): Table
{
return $table
...
->inverseRelationship('exercisesRelatedTo');
}
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.
Patrick Boivin
Patrick Boivin17mo ago
Awesome, that's good to know! I'm sure it will be useful again in the future.
Want results from more Discord servers?
Add your server