Getting count from pivot table in laravel eloquent

前端 未结 4 1658
青春惊慌失措
青春惊慌失措 2020-12-17 01:25

I have a many to many relationship for orders and products.



        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-17 01:51

    If you already have the $products object, you can do the following:

    $rolecount = $products->roles()->count();
    

    Or if you are using eager loading:

    $rolecount = $products->roles->count();
    

    Cheers.

提交回复
热议问题