I am building an admin panel using Laravel nova. I have an Items table, that has a belongsToMany to itself, using (of course) a pivot table.
This in itself is working, but when I specify that item A is related to item B, the inverse of that relation is not stored, so that results in not being found on item B.
I need it to, because the relationship is symmetrical, meaning that if A relates to B, B also relates to A.
What would be the best way to store and retrieve the relation?
This is my current code that defines the relation, and that works one way.
class Item extends Model
{
public function related(): BelongsToMany
{
return $this->belongsToMany(Item::class, 'item_related', 'item_id', 'related_id');
}
}
So: imagine I have a table with the items A, B and C, and the relations are defined as such in the pivot table
item_id - related_id
A - B
A - C
B - C
So basically, when I do B->related, I want it to return [A, C]. Now it will only return C, and C does not have any related items at all
There are two possible solutions here, one being to insert the inverse relation as well, resulting in
item_id - related_id
A - B
B - A
A - C
C - A
B - C
C - B
This will yield the correct results, but how to automatically force laravel to save the inverted relation as well, and making sure that deletions etc are correctly done?
The other option would be is 'merging' two related belongsToMany calls.
class Item extends Model
{
public function related(): BelongsToMany
{
// return merged $this->relatedFrom and $this->relatedTo
}
public function relatedFrom(): BelongsToMany
{
return $this->belongsToMany(Item::class, 'item_related', 'related_id', 'item_id');
}
public function relatedTo(): BelongsToMany
{
return $this->belongsToMany(Item::class, 'item_related', 'item_id', 'related_id');
}
}
This would also cause all kind of problems.
So what would be a good approach here?