The best approach would be to create a categories table and a products table.
categories : id, name
products : id, name, category_id
Then you define the relationships in the models.
// Category model
public function $products()
{
return $this->hasMany(Product::class);
}
// Product model
public function $category()
{
return $this->belongsTo(Category::class);
}
Once this is done, you can these relationships in the controller.
If you already have a category and you want to load all products in that category.
// This could be fine for a URL like https://mydomain.dev/drinks/2/products
// Here 2 is the id of the category for which you want to display the products
$products = $category->products;
If you have a list of products and you want to know the category for each one.
$products = Product::with('category')->get();