dongzong7467 2015-10-04 04: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部