F
Filament9mo ago
Mina

filter query join is not working for me

so, I am trying to make a filter with join on another table. The query is working fine but for some reason it's not working in filament. Do you have any idea why and if this is even possible to do, I really need this functionality on my page: Tables\Filters\Filter::make('least_money_made')->query(function (Builder $query) { return $query->select('funnels.*') ->leftJoin('orders', 'funnels.id', '=', 'orders.funnel_id') ->selectRaw('COALESCE(SUM(orders.total_price), 0) AS total_money_made') ->groupBy('funnels.id') ->orderBy('total_money_made', 'ASC') ->limit(1); });
3 Replies
Mina
MinaOP9mo ago
⬆️ i've also tried with relationship, and i didn't manage to get it to work: Tables\Filters\Filter::make('least_money_made')->query(function (Builder $query) { return $query->withCount('orderRelation') ->with(['orderRelation' => function ($query) { $query->selectRaw('funnel_id, SUM(total_price) as total_money_made') ->groupBy('funnel_id'); }]) ->get() ->sortBy(function ($funnel) { return $funnel->orderRelation_count > 0 ? $funnel->orderRelation->sum('total_money_made') : PHP_INT_MAX; }) ->first(); }),
Dennis Koch
Dennis Koch9mo ago
Check the resulting query with somethng like Debugbar. I guess the issue is that Filters are wrapped in ->where()s. But there should be a baseQuery or something similar method which maybe works for applying the left join.
Mina
MinaOP9mo ago
I will try to do that, the query checks out, i get the result I've also tried with the baseQuery method and it doesn't work I couldn't make this one work, so we moved it into a widget because we need this info from anywhere
Want results from more Discord servers?
Add your server