Jun 4, 2024
0
Level 1
I have BelongsToMany same model by pivot table ,need both side relationship in one function
Hi, I use filament and selector for analogs, the problem is when the relation between products is created is displayed just one way in my case analogs should be displayed in both directions, I just started with Laravel and filament, didn't find any related info.
This I have at the moment is working : Model:
public function analogs()
{
return $this->belongsToMany(Product::class, 'product_analogs', 'product_id', 'analog_id');
}
public function analogsOf()
{
return $this->belongsToMany(Product::class, 'product_analogs', 'analog_id', 'product_id');
}
Filament resource:
Forms\Components\Select::make('analogs')
->relationship('analogs', 'name')
->label('Analog Products')
->multiple()
Migration:
Schema::create('product_analogs', function (Blueprint $table) {
$table->id();
$table->unsignedBiginteger('product_id');
$table->unsignedBiginteger('analog_id');
$table->foreign('product_id')->references('id')->on('products')->cascadeOnDelete();
$table->foreign('analog_id')->references('id')->on('products')->cascadeOnDelete();
$table->timestamps();
});
how I understated instead of functions analogs() and analogsOf() need one function which combines both or may know another method
Please or to participate in this conversation.