dongzheng7165 2016-12-17 23:03
浏览 126
已采纳

Laravel Eloquent whereIn,从数据库中检索记录,包括缺少日期

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 :)

  • 写回答

3条回答 默认 最新

  • dqwyghl0649 2016-12-17 23:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号