I am new to Laravel.
I am building app that counts your daily steps.
And I am building API, so I need to return 0 steps when there are no results in DB for selected days in array.
But if there is data for some of the days to return actual steps and to return 0 for other days.
I have tried this:
$steps = Step::whereIn('date', $dates)
->where('user_id', Auth::id())
->get();
But it returns only matches. As I wrote I want to return data for all days, not only for days in DB. Please, help :)
This will never be possible with a simple Query as the DB can not give you anything, it does not know about. You can solve this either progrmmatically bei iterating over the result and adding all missing dates with 0-value or by adding a table to your DB which contains all dates and then left join your steps table with it in the query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With