title: How to output odd and even rows in laravel
body: <p>I have a project where I'm pulling posts from the database and rendering on the home page view. Like a Blog. (On the home page I limit this to 3 posts)</p>
  
  <p>I have 6 rows in the table, and would like to style the output based on ODD and Even rows.</p>
  
  <p><strong>Here is my controller:</strong></p>
  
  <pre><code>public function index()
  {
     $counter = Post::count();
     $posts= DB::table('posts')-&gt;orderBy('id', 'DESC')-&gt;limit(3)-&gt;get();
  
      return view('home',compact('posts','counter')); 
  }
  </code></pre>
  
  <p>I want the even numbered rows to have <code>&lt;div class="even"&gt;</code> and the odd numbered rows to have <code>&lt;div class="odd"&gt;</code></p>
  
  <p>When I <code>dd</code> on <code>$counter</code> I get the value 6. This is correct. I have 6 rows in the table.</p>
  
  <p>What I'm currently trying based on other articles I've found:</p>
  
  <pre><code>@foreach ($posts as $post)
  @if($counter % 2 == 0)
  &lt;div class="even"&gt;{{$post-&gt;title}}&lt;/div&gt;
  @else
  &lt;div class="odd"&gt;{{$post-&gt;title}}&lt;/div&gt;
  @endif
  </code></pre>
  
  <p>This doesn't do anything. Still outputs the rows as 6,5,4,3,2,1</p>
  
  <p>So how can I write the IF Statement inside my Foreach loop to say...</p>
  
  <pre><code>if ($counter == odd)
  &lt;div class="odd"&gt;
  else
  &lt;div class="even"&gt;
  </code></pre>
  
  <p>The order I'm looking for is:</p>
  
  <ul>
  <li>Odd</li>
  <li>Even</li>
  <li>Odd</li>
  <li>Even</li>
  </ul>