title: Using Laravel Eloquent need to get all products according to category id
body: <p>I need to get all products according to its category id.
  This is my controller.</p>
  <p><strong>UserProductsController</strong></p>
  <pre><code>class UserProductsController extends Controller
  {
      public function index()
      {
          $products = Product::get();
          return view ('products')-&gt;with(compact('products'));
        
      }
        
      public function product_categories()
      {
          $categories = Category::all();
          return view ('categories')-&gt;with(compact('categories'));
        
      }
  
  
      public function products(Category $category)
      {
          $category-&gt;load('products');
  
          return view('categorize')-&gt;withCategory($category);
      } 
    
  
  }
  </code></pre>
  <p><strong>This is my blade file.</strong></p>
  <pre><code>    @foreach($category as $c) 
       &lt;div class=&quot;mb-2&quot; class=&quot;font-size-12 text-gray-5&quot;&gt;{{ $c ['cat_name'] }}&lt;/a&gt;&lt;/div&gt;
          &lt;h5 class=&quot;mb-1 product-item__title&quot; class=&quot;text-blue font-weight-bold&quot;&gt;{{ $c ['prod_name'] }}&lt;/a&gt;&lt;/h5&gt;
              &lt;li class=&quot;line-clamp-1 mb-1 list-bullet&quot;&gt;{{ $c ['prod_description'] }}&lt;/li&gt;
              &lt;div class=&quot;text-gray-20 mb-2 font-size-12&quot;&gt;{{ $c ['prod_item_code'] }}&lt;/div&gt;
              &lt;div class=&quot;flex-center-between mb-1&quot;&gt;
                  &lt;div class=&quot;prodcut-price&quot;&gt;
                     &lt;div class=&quot;text-gray-100&quot;&gt;LKR {{ $c ['prod_price'] }}.00&lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  @endforeach
  </code></pre>
  <p><strong>This is my route file.</strong></p>
  <pre><code>Route::get('/categorize/{category_id}', 'UserProductsController@products')-&gt;name('categorize');
  </code></pre>
  <p>This is the <strong>Categories</strong> table.</p>
  <pre><code>public function up()
      {
         Schema::create('categories', function (Blueprint $table)
          {
          $table-&gt;bigIncrements('id');
          $table-&gt;unsignedBigInteger('parent_id')-&gt;nullable();
          $table-&gt;string('cat_name')-&gt;nullable();
          $table-&gt;string('cat_image_path')-&gt;nullable();
          $table-&gt;timestamps();
           });
      }
  </code></pre>
  <p>This is my <strong>Products</strong> table.</p>
  <pre><code>public function up()
      {
          Schema::create('products', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;string('prod_name');
              $table-&gt;string('prod_brand')-&gt;nullable();
              $table-&gt;unsignedBigInteger('category_id');
              $table-&gt;string('prod_description')-&gt;nullable();
              $table-&gt;string('prod_item_code')-&gt;nullable();
              $table-&gt;string('prod_modal')-&gt;nullable();
              $table-&gt;string('prod_size')-&gt;nullable();
              $table-&gt;string('prod_weight')-&gt;nullable();
              $table-&gt;string('prod_height')-&gt;nullable();
              $table-&gt;string('prod_manufacturer')-&gt;nullable();
              $table-&gt;float('prod_price')-&gt;nullable();
              $table-&gt;float('prod_discount')-&gt;nullable();
              $table-&gt;float('prod_quantity')-&gt;nullable();
              $table-&gt;string('prod_image_path')-&gt;nullable();
              $table-&gt;timestamps();
  
              $table-&gt;foreign('category_id')
              -&gt;references('id')
              -&gt;on('categories')
              -&gt;onDelete('cascade');
          });
      }
  </code></pre>
  <p>This is my <strong>category</strong> model.</p>
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  
  class Category extends Model
  {
      public function products()
      {
         return $this-&gt;hasMany(Product::class);
      }
  }
  </code></pre>
  <p>This is my <strong>product</strong> model.</p>
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  use Storage;
  
  class Product extends Model
  {
      public function category()
      {
          return $this-&gt;belongsTo('App\Category');
      }
  }
  </code></pre>
  <p>When I select the particular category my URL is directing me to that category_id's page, but the data doesn't pass. What could be the problem? Anyone please direct me?</p>