Subquery in table filter
this is the query:
im trying to show the song name, and how many times the song is listened. like this, it works. the problem comes when i want to filter by certain user:
it just doesnt work. the field to be filtered is the
user_song_play_history.user_id
. is this the right way to do it?
if i manually filter it (uncommenting the commented line in the query fn) works, but if i filter via the gui, it doesnt
old question: https://discord.com/channels/883083792112300104/120413699228840761215 Replies
no one? plz let me know if i should re ask it better / provide more code so is easy to understand my question ^^
I think you need to rephrase your question to something like
Subquery in table filter, that is wat you need in my opinion.
thanks, lets see now ^^
what happens when you log $data['value'] somewhere? What is the value of it when you filter on a user?
You can do something like error_log($data['value']) and it logs it into the terminal where you have served your local environment
if i remember well was null always
cant test it now cuz i dont have the project here, but when i can ill retry it
didnt know this trick - error_log()
but im running it via nginx not php artisan serve
You should be able to dump the result somewhere! dd() will not work i think because it will run on page load and will not let you set the filter
okay, ill use
log
then yeah, to log it in laravel.log file
so last time i used dd, that is maybe why always was nullDont know for sure that it will block your filter, but can be a start...
in a few hours ill be able to test it again, ill post the results here
this is what im getting when i filter
You are missing the
use ($data)
with the function ($q)
thats why you are getting the error.🤦♂️
okay now:
with this code:
in
laravel.log
:
but the table doesnt change, i see the same records & same values. it doesnt changereturn $query
->when(($data['value'] ?? null), function (Builder $query) use ($data) {
$query->withCount([
'plays' => function ($q) use ($data) {
$q->where('is_listened', true);
$q
},
]);
})->where('user_song_play_history.user_id', $data['value']);
I am not sure but dont you want the user to be filtered outside of the when clause? The user filter has nothing to do with the count you are making right
I am not sure but dont you want the user to be filtered outside of the when clause? The user filter has nothing to do with the count you are making right
if i filter outside im filtering in the songs table, i want to filter the
user_id
pivot fieldno ideas? (: