title: Why is the article count variable zero on all pages but the first, in this Laravel app?
body: <p>I am working on a <strong><a href="https://github.com/Ajax30/BraveCMS-2.0/pull/17/files" rel="nofollow noreferrer">blogging application</a></strong> in Laravel 8.</p>
  <p>The <strong>FrontendController</strong> controller contains the data that is common to all the theme's views:</p>
  <pre><code>namespace App\Http\Controllers;
  use App\Models\Settings;
  use App\Models\ArticleCategory;
  use App\Models\Page;
  class FrontendController extends Controller
  {
      protected $data;
      protected $site_settings;
      protected $theme_directory;
      protected $site_name;
      protected $tagline;
      protected $owner_name;
      protected $owner_email;
      protected $twitter;
      protected $facebook;
      protected $instagram;
      protected $is_cookieconsent;
      protected $is_infinitescroll;
      protected $pages;
      protected $article_categories;
  
      public function __construct()
      {
          $this-&gt;site_settings = Settings::first();
          $this-&gt;theme_directory = $this-&gt;site_settings['theme_directory'] ?? null;
          $this-&gt;site_name = $this-&gt;site_settings['site_name'] ?? null;
          $this-&gt;tagline = $this-&gt;site_settings['tagline'] ?? null;
          $this-&gt;owner_name = $this-&gt;site_settings['owner_name'] ?? null;
          $this-&gt;owner_email = $this-&gt;site_settings['owner_email'] ?? null;
          $this-&gt;twitter = $this-&gt;site_settings['twitter'] ?? null;
          $this-&gt;facebook = $this-&gt;site_settings['facebook'] ?? null; 
          $this-&gt;instagram = $this-&gt;site_settings['instagram'] ?? null; 
          $this-&gt;is_cookieconsent = $this-&gt;site_settings['is_cookieconsent'] ?? null;
          $this-&gt;is_infinitescroll = $this-&gt;site_settings['is_infinitescroll'] ?? null;
  
  
          // Article categories
          $this-&gt;article_categories = ArticleCategory::all();
  
                  // Pages
          $this-&gt;pages = Page::all();
  
          $this-&gt;data = [
            'theme_directory' =&gt; $this-&gt;theme_directory,
            'site_name' =&gt; $this-&gt;site_name,
            'tagline' =&gt; $this-&gt;tagline,
            'owner_name' =&gt; $this-&gt;owner_name,
            'owner_email' =&gt; $this-&gt;owner_email,
            'twitter' =&gt; $this-&gt;twitter,
            'facebook' =&gt; $this-&gt;facebook, 
            'instagram' =&gt; $this-&gt;instagram,
            'is_cookieconsent' =&gt; $this-&gt;is_cookieconsent,
            'is_infinitescroll' =&gt; $this-&gt;is_infinitescroll,
            'pages' =&gt; $this-&gt;pages,
            'categories' =&gt; $this-&gt;article_categories,
          ];
      }
  }
  </code></pre>
  <p>The <strong>ArticlesController</strong> controller extends the one above:</p>
  <pre><code>namespace App\Http\Controllers;
  use Illuminate\Http\Request;
  use App\Models\User;
  use App\Models\ArticleCategory;
  use App\Models\Article;
  
  class ArticlesController extends FrontendController {
  
      // Articles per page
      protected $per_page = 12;
  
      public function index(Request $request) {
  
          // Search query
          $qry = $request-&gt;input('search');
  
          $articlesQuery = Article::where('title', 'like', '%' . $qry . '%')
                                              -&gt;orWhere('short_description', 'like', '%' . $qry . '%')
                                              -&gt;orWhere('content', 'like', '%' . $qry . '%');
  
          $articles = $articlesQuery-&gt;orderBy('id', 'desc')-&gt;paginate($this-&gt;per_page);
  
          // Search results count
          if ($request-&gt;input('search')) {
              $article_count = $articlesQuery-&gt;count();
          }       
  
          return view('themes/' . $this-&gt;theme_directory . '/templates/index', 
              array_merge($this-&gt;data, [
                  'search_query' =&gt; $qry,
                  'articles' =&gt; $articles,
                  'article_count' =&gt; $article_count ?? null
              ])
          );
      }
      
  }
  </code></pre>
  <p>The posts list view (index.blade.php):</p>
  <pre><code>&lt;div class=&quot;col-lg-8 col-md-10 mx-auto&quot;&gt;
  
      @if (isset($search_query))
          &lt;p class=&quot;mt-0 text-muted&quot;&gt;We found {{ $article_count }} posts containing &lt;span class=&quot;quote-inline&quot;&gt;{{ $search_query }}&lt;/span&gt;:&lt;/p&gt;
      @endif
  
      @if (count($articles))
          @foreach ($articles as $article)
              &lt;div class=&quot;post-preview&quot;&gt;
                  &lt;a href=&quot;{{ url('/show/' . $article-&gt;slug) }}&quot;&gt;
                      &lt;h2 class=&quot;post-title&quot;&gt;
                          {{ $article-&gt;title }}
                      &lt;/h2&gt;
                      &lt;h3 class=&quot;post-subtitle&quot;&gt;
                          {{ $article-&gt;short_description }}
                      &lt;/h3&gt;
                  &lt;/a&gt;
                  &lt;p class=&quot;post-meta&quot;&gt;Posted by
                      &lt;a href=&quot;{{ url('/author/' . $article-&gt;user-&gt;id) }}&quot;&gt;{{ $article-&gt;user-&gt;first_name }} {{ $article-&gt;user-&gt;last_name }}&lt;/a&gt;
                      on {{ date('j F, Y', strtotime($article-&gt;created_at)) }}
                  &lt;/p&gt;
              &lt;/div&gt;
  
              @if(!$loop-&gt;last)&lt;hr&gt;@endif
              
          @endforeach
      @endif
  
      &lt;!-- Pager --&gt;
      @if($articles-&gt;hasPages())
          &lt;div class=&quot;clearfix&quot;&gt;
              &lt;ul class=&quot;pagination&quot;&gt;
                  &lt;li class=&quot;next&quot;&gt;
                      &lt;a class=&quot;btn btn-primary {{ $articles-&gt;onFirstPage() ? 'disabled' : '' }}&quot; href=&quot;{{ $articles-&gt;withQueryString()-&gt;previousPageUrl() }}&quot;&gt;&amp;larr; Newer Posts&lt;/a&gt;
                  &lt;/li&gt;
                  &lt;li class=&quot;prev&quot;&gt;
                      &lt;a class=&quot;btn btn-primary {{ $articles-&gt;onLastPage() ? 'disabled' : '' }}&quot; href=&quot;{{ $articles-&gt;withQueryString()-&gt;nextPageUrl() }}&quot;&gt;Older Posts &amp;rarr;&lt;/a&gt;
                  &lt;/li&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      @endif
  &lt;/div&gt;
  </code></pre>
  <h3>The problem</h3>
  <p>Whenever there are paginated search results, the <code>$article_count</code> variable displays the total article count only on the first page. On all the others, <strong>it shows 0</strong>.</p>
  <h3>Questions</h3>
  <ol>
  <li>What causes this bug?</li>
  <li>What is the easiest fix?</li>
  </ol>