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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?