F
Filament3mo ago
adnn

Many relationship manager?

In my database I have brands and products I have a resource called Brand, and have tried making a relationship manager for Products. However, on my Brands table I don't have any product ids, because it can have hundreds. my products table has a brand_id column How do i properly set up this relationship in filament? Do I need to create an in between table?
Solution:
php artisan make:filament-relation-manager BrandResource products name Brand Model ```php public function products(): HasMany...
Jump to solution
8 Replies
Jonay.Medina
Jonay.Medina3mo ago
you can use: belongsTo -> brand in product model and set in ProductResource under $form->schema something like this
Forms\Components\Select::make('brand_id')
->label("Brand")
->relationship('branch', 'name'),
Forms\Components\Select::make('brand_id')
->label("Brand")
->relationship('branch', 'name'),
and on productResource -> table->columns
->columns([
Tables\Columns\TextColumn::make('brand.name')
->label('brands')
->sortable(),
->columns([
Tables\Columns\TextColumn::make('brand.name')
->label('brands')
->sortable(),
No description
Jonay.Medina
Jonay.Medina3mo ago
now if do you want see al products that has one brand, Leandro Ferreira message can solve
adnn
adnn3mo ago
this works if brand has a product id column, however if it doesnt it just says brand::product() doesn't exist or something like that.
LeandroFerreira
LeandroFerreira3mo ago
Do you mean, product has a brand_id?
adnn
adnn3mo ago
yeah brands table doesn't have anything, relating it to products but product has brand_id. I have a Brand resource, am trying to make it so I can click on a Brand and still list its product where brand_id = selected_id or something like that
Solution
LeandroFerreira
LeandroFerreira3mo ago
php artisan make:filament-relation-manager BrandResource products name Brand Model
public function products(): HasMany
{
return $this->hasMany(Product::class);
}
public function products(): HasMany
{
return $this->hasMany(Product::class);
}
Product Model
public function brand(): BelongsTo
{
return $this->belongsTo(Brand::class);
}
public function brand(): BelongsTo
{
return $this->belongsTo(Brand::class);
}
adnn
adnn3mo ago
This worked, thank you so much, I really appreciate it!!