title: How to implement my sql statement in Laravel?
body: <p>I have the following MySQL query which works fine. It returns Random results from my table.</p>
  
  <pre><code>SET @prev=0,@rownum=0;
  SELECT utilizador_id, nome 
  FROM (
    SELECT *, 
           IF( @prev &lt;&gt; utilizador_id, 
               @rownum := 1, 
               @rownum := @rownum+1 
           ) AS rank, 
           @prev := utilizador_id, 
           @rownum  
    FROM (
      SELECT * FROM anuncios 
      ORDER BY utilizador_id, rand()
    ) AS random_ads
  ) AS ads_ranked 
  WHERE rank &lt;= 2;
  </code></pre>
  
  <p><strong>Here is my table:</strong></p>
  
  <pre><code>+-------------+------+
  |utilizador_id|nome  |
  +-------------+------|
  |     1       |test1 |
  |     1       |test2 |
  |     1       |test3 |
  |     1       |test4 |
  |     1       |test5 |
  |     2       |test1 |
  |     2       |test2 |
  |     2       |test3 |
  |     3       |test1 |
  |     3       |test2 |
  |     3       |test3 |
  +-------------+------|
  </code></pre>
  
  <p><strong>Expected Random results:</strong></p>
  
  <pre><code>+-------------+------+
  |utilizador_id|nome  |
  +-------------+------|
  |     1       |test2 |
  |     1       |test5 |
  |     2       |test1 |
  |     2       |test2 |
  |     3       |test1 |
  |     3       |test3 |
  +-------------+------|
  </code></pre>
  
  <p>The sql statement as mention works fine in MySQL but I want to implement it in my Laravel environment.</p>
  
  <p><strong>My Question:</strong> How do I implement this sql statement in Laravel?</p>