F
Filament16mo ago
Aprod

addSelect is not working on $table->query()

I'm trying to select a custom column in my query, but ->addSelect on the query broke the table. Is there other option to select a custom column?
22 Replies
krekas
krekas16mo ago
No errors?
Aprod
AprodOP16mo ago
Aprod
AprodOP16mo ago
I think the problem is thet when I add addSelect to the query, it overwrites the default selects This is my query
return Ingredient::query()
->addSelect([DB::raw('total * price as total_price')]);
return Ingredient::query()
->addSelect([DB::raw('total * price as total_price')]);
krekas
krekas16mo ago
If I remember correctly it makes a second query and doesn't overwrite other selects
Aprod
AprodOP16mo ago
If I add all the fields that I'm using it is working
return Ingredient::query()
->select([
'id',
DB::raw('total * price as total_price'),
'name',
'totals.total as total',
'price'
]);
return Ingredient::query()
->select([
'id',
DB::raw('total * price as total_price'),
'name',
'totals.total as total',
'price'
]);
but this would be the meaning of addSelect to not overwrite current selects just add a new one
krekas
krekas16mo ago
this is for the view record?
Aprod
AprodOP16mo ago
This is a table
krekas
krekas16mo ago
try return parent::getEloquentQuery()->addSelect([DB::raw('total * price as total_price')])
Aprod
AprodOP16mo ago
This has no parent::getEloquentQuery
krekas
krekas16mo ago
where do you put your code? which file? which method?
Aprod
AprodOP16mo ago
This is a simple livewire component I added HasTable and InteractsWithTable
krekas
krekas16mo ago
you are on v2?
Aprod
AprodOP16mo ago
v3
krekas
krekas16mo ago
i don't get it then where you add query
Aprod
AprodOP16mo ago
krekas
krekas16mo ago
but you show query with return
Aprod
AprodOP16mo ago
the query method first parameter can be a clouser that returns a query This is my whole query, I just simplied it down to be more understandable
Aprod
AprodOP16mo ago
Aprod
AprodOP16mo ago
Right now it works with the select but then I loses features such as groups on table
krekas
krekas16mo ago
your query is just a mess why do you need those joins? filament should eager load everything
Aprod
AprodOP16mo ago
@krekas this is some aggregated data, I have event with days with meals with recipes with ingredients, and I need a list where all the ingredients are listed that are required for an event, and also the amount of the ingredient summed up. Yes, we could do a solution to eagerload and then calculate in PHP these sums, but that way you cannot order by the sums, so the sum needs to be in the query. Anyways if I remove the $join and only create an addSelect it is still not working, so that is why I simplied it down to be more understandable
Want results from more Discord servers?
Add your server