Laravel Eloquent how to get all products in a category with slug
<p>My Productcategory.php has</p>

<pre><code>public function products()
{
    return $this-&gt;hasMany('App\Models\Product');
}
</code></pre>

<p>And Product.php has</p>

<pre><code>public function productcategory()
{
    return $this-&gt;belongsTo('App\Models\Productcategory', 'category_id');
}
</code></pre>

<p>Now my route is </p>

<pre><code>Route::get('gallery/{slug}', 'GalleryController@index');
</code></pre>

<p>When URL is something like <code>gallery/print-pattern-client-work</code>, how can I get all products with the same category? I have the following but <code>category_id</code> is an integer and not a slug. So I am not so sure how to do it.</p>

<pre><code>public function index()
{
    $categoryslug = Request::segment(2);
    $products = Productcategory::with('products')-&gt;where('category_id',$categoryslug)-&gt;get();
...
}
</code></pre>