dongzong7467 2015-10-04 12:51
浏览 13
已采纳

Laravel 4:计算其中的关系

I have 2 tables as below:

Subscription table have a entrance_limit column, and it has a hasMany('Attendance') relationship. The entrance_limit column will actually limit the number of rows in Attendance. For example, if the entrance_limit value is 10, then we only can create 10 rows in Attendance.

Attendance table has a belongsTo('Subscription') relationship.

How do I get the list of Subscription that have total number of Attendance less than the entrance_limit value?

Subscription::whereHas('attendances', function($q) {
    ## something like this
    $q->count() < subscription->entrance_limit
})
  • 写回答

2条回答 默认 最新

  • douyong4623 2015-10-05 13:36
    关注

    Finally able to get it working with the following:

    Subscription::whereRaw('
        (select count(*) from `attendances` where `attendances`.`subscription_id` = `subscriptions`.`id`) < `subscriptions`.`entrance_limit`
    ');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable