Laravel many to many loading related models with count
问题 I am trying to link 4 tables and also add a custom field calculated by counting the ids of some related tables using laravel. I have this in SQL which does what I want, but I think it can be made more efficient: DB::select('SELECT posts.*, users.id AS users_id, users.email,users.username, GROUP_CONCAT(tags.tag ORDER BY posts_tags.id) AS tags, COUNT(DISTINCT comments.id) AS NumComments, COUNT(DISTINCT vote.id) AS NumVotes FROM posts LEFT JOIN comments ON comments.posts_id = posts.id LEFT JOIN