title: How can I select count from relational table while using with laravel
body: <p>I am working for a social app, I need to show feeds with tagged user, hash tags, like counts, comments count, comments and users details who has commented.</p>
  
  <p>The feed is bit different from normal feed, here is the database structure.</p>
  
  <p><strong>User Table:</strong></p>
  
  <p><code>id</code> <code>username</code> <code>email</code> <code>image</code></p>
  
  <p><strong>Movie Table:</strong> (Admin will add the movie, and user will rate that by search a movie by title)</p>
  
  <p><code>id</code> <code>title</code> <code>image</code> <code>description</code></p>
  
  <p><strong>Rating Table:</strong> (this will be a feed, that <code>a</code> user has rated <code>x</code> movie, <code>while rating user will tag to another user</code> and also use some <code>hash tags</code>)</p>
  
  <p><code>id</code> <code>movie_id</code> <code>user_id</code> <code>rating</code> <code>description</code></p>
  
  <p><strong>rating_tag table:</strong> (it will hold the hash tags id and rating id)</p>
  
  <p><code>id</code> <code>rating_id</code> <code>tag_id</code></p>
  
  <p><strong>rating_user table:</strong> (it will hold the tagged users id and rating id)</p>
  
  <p><code>id</code> <code>rating_id</code> <code>tag_id</code></p>
  
  <p><strong>likes table:</strong></p>
  
  <p><code>id</code> <code>rating_id</code> <code>user_id</code></p>
  
  <p><strong>comments table:</strong></p>
  
  <p><code>id</code> <code>rating_id</code> <code>user_id</code> <code>comment</code></p>
  
  <p><strong>Now I need to show feed with all the stuff, rating as feed (user image who has rated, movie image, title and description) with all comments that who has commented with those users details and total like count.</strong></p>
  
  <p>I have use that but its not giving me proper result.</p>
  
  <pre><code>public function getAllForFeed ($user_ids) {
      return $this-&gt;with(['tags' =&gt; function ($q) {
          $q-&gt;select('tags.id');
      }])-&gt;with(['likes' =&gt; function ($q) {
          $q-&gt;select(DB::raw('count(id) as like_count'));
      }])-&gt;with(['comments' =&gt; function ($q) {
          $q-&gt;select('user_id', 'comment');
      }])-&gt;with(['users' =&gt; function ($q) {
          $q-&gt;select('users.id');
      }])-&gt;whereIn('user_id', $user_ids)-&gt;get()-&gt;toArray();
  }
  </code></pre>