title: Fetch records base on previous ID - Laravel
body: <p>Please I want to fetch crops based on its <code>category id</code>, will be glad if this can be achieved without JavaScript</p>
  
  <p><strong>Display page</strong></p>
  
  <pre><code>&lt;div class="cat-list"&gt;
      &lt;h3 class="cat-title"&gt;
          &lt;a href="/filterByCategory/{{$category-&gt;cat_id}}"&gt;
              &lt;i class="fa fa-car ln-shadow"&gt;&lt;/i&gt;
              {{$category-&gt;cat_name}}&lt;span class="count"&gt;{{$category-&gt;count()}}&lt;/span&gt;
          &lt;/a&gt;
          &lt;span data-target=".cat-id-1" data-toggle="collapse" class="btn-cat-collapsed collapsed"&gt;
              &lt;span class=" icon-down-open-big"&gt;&lt;/span&gt;
          &lt;/span&gt;
      &lt;/h3&gt;
      &lt;ul class="cat-collapse collapse in cat-id-1"&gt;
      @php
          $catID = $category-&gt;cat_id;
          //want to fetch crops base on the $catID and display in foreach
      @endphp
      &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Controller function</strong></p>
  
  <pre><code>public function index()
  {
      $categories = Category::all();
  
      $crops = Crop::all();
  
      return view('public.index' , compact('categories' , 'crops'));
  }
  </code></pre>
  
  <p><strong>Crop Model</strong></p>
  
  <pre><code>public function category() 
  { 
      return $this-&gt;belongsTo(Category::class, 'cat_id', 'crop_cat')
  }
  </code></pre>
  
  <p><strong>Category Model</strong></p>
  
  <pre><code>public function category() 
  { 
      return $this-&gt;hasmany(Category::class, 'crop_cat','cat_id') 
  }
  </code></pre>