F
Filament8mo ago
Wim

Favorite products: many to many not working

I have an app where users can favor many products. This is done via a many to many relation with a Favorite pivot table. Adding things into the database works. The favorites table is as follows:
id | user_id | product_id
1 | 2 | 2
2 | 2 | 4
id | user_id | product_id
1 | 2 | 2
2 | 2 | 4
In Filament I have a Favorites resources where the idea is the user can get an overview of the products he's favoured. In a Filament table I have the following:
return $table
->columns([
Tables\Columns\TextColumn::make('user.name'),
Tables\Columns\TextColumn::make('product.name'),
return $table
->columns([
Tables\Columns\TextColumn::make('user.name'),
Tables\Columns\TextColumn::make('product.name'),
The username is printed but the product name is not. Any idea how to do this? I keep on struggling with this. Some more information in case relevant: User Model:
public function favorites()
{
return $this->belongsToMany(Product::class, 'favorites');
}
public function favorites()
{
return $this->belongsToMany(Product::class, 'favorites');
}
Product model:
public function favorites()
{
return $this->belongsToMany(User::class, 'favorites');
}
public function favorites()
{
return $this->belongsToMany(User::class, 'favorites');
}
Favorite model:
public function user()
{
return $this->belongsTo(User::class);
}


public function product()
{
return $this->belongsTo(Product::class);
}
public function user()
{
return $this->belongsTo(User::class);
}


public function product()
{
return $this->belongsTo(Product::class);
}
3 Replies
Povilas K
Povilas K8mo ago
You should install Laravel Debugbar and see what SQL queries are executed under the hood, and see if the correct products are queried at all. Could they be soft-deleted or something?
Wim
WimOP8mo ago
Thanks Povilas. I did follow your suggestion and found indeed something interesting. The query that gets executed is
select * from `products` where `products`.`id` in (2, 36) and `products`.`organization_id` in (2)
select * from `products` where `products`.`id` in (2, 36) and `products`.`organization_id` in (2)
I have a multi-tenancy setup and by default Filament is configured to only show the products that are part of the organization tenant. When I change the products (manually in the database) to belong to the logged in organization, the data will show correctly. When the products belong to other organizations, the information does not show. I have a middleware
ApplyTenantScopes
ApplyTenantScopes
as follows:
public function handle(Request $request, Closure $next): Response
{
Animal::addGlobalScope(
fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant()),
);

return $next($request);
}
public function handle(Request $request, Closure $next): Response
{
Animal::addGlobalScope(
fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant()),
);

return $next($request);
}
Not sure if there is a way to apply the global scope to all resources, except Favorites? Or perhaps there's better suggestions? Tried with
protected static bool $isScopedToTenant = false;
protected static bool $isScopedToTenant = false;
in the FavoriteResource but does not work
Povilas K
Povilas K8mo ago
Sorry I don't have a quick answer without experimenting and debugging
Want results from more Discord servers?
Add your server