title: Eloquent/SQL - Get the row number of a query (rank)
body: <p>I would like to know the rank based on my DB structure:</p>
  
  <p>I have a model <code>Post</code> that belongs to a model called <code>Edition</code> (also <strong>one</strong> <code>Edition</code> <strong>has many</strong> <code>Post</code>).</p>
  
  <p>One <code>Post</code> has many <code>Like</code>.</p>
  
  <p>I would like to know the rank of a <code>Post</code> based on the <code>Like</code> count inside a particular <code>Edition</code>.</p>
  
  <p>The code:</p>
  
  <pre><code>// Define the id of an edition
  $edition_id = 53;
  // The id of the post to retrieve rank
  $post_id = 132;
  // Compose the query by select all posts, of interested edition ...
  $query = App\Models\Post::where('edition_id', $edition_id)
      // ... with like count (this produce an additional field named likes_count) ...
      -&gt;withCount('likes')
      // ... in descendig order by this count.
      -&gt;orderBy('likes_count', 'desc');
  // By execute it I can get right results.
  $query-&gt;get();
  </code></pre>
  
  <p>For people who are not familiar with <code>Laravel Eloquent ORM</code>, I report the sql query executed from code above:</p>
  
  <pre><code>select `posts`.*, (select count(*) from `likes` where `posts`.`id` = `likes`.`post_id` and `likes`.`deleted_at` is null) as `likes_count`
  from `posts`
  where `edition_id` = '53' and `posts`.`deleted_at` is null
  order by `likes_count` desc
  </code></pre>
  
  <p>I report the query results:</p>
  
  <pre><code>=&gt; Illuminate\Database\Eloquent\Collection {#994
  all: [
      App\Models\Post {#993
          id: 135,
          likes_count: 4,
      },
      App\Models\Post {#1075
          id: 134,
          likes_count: 3,
      },
      App\Models\Post {#1091
          id: 133,
          likes_count: 2,
      },
      App\Models\Post {#997
          id: 132,
          likes_count: 1,
      },
      App\Models\Post {#1038
          id: 131,
          likes_count: 0,
      },
  ],
  }
  </code></pre>
  
  <p>How can I get the row position of the record with a certain <code>id</code> from the results of the composed query?</p>
  
  <p>For example, how to retrieve the rank result of the record with <code>id = 132</code>?</p>