Muhammad Ramzan Muhammad Ramzan - 4 months ago 14
Ajax Question

I want to show foods based on categories in laravel

I have a database table categories having columns:


  • Category_id

  • CategoryName



And other table is Food with:


  • Food_id

  • FoodName

  • Category_id



I want to show the food items according to category on webpage.
I'm using laravel 5.2 and I'm newbie.

Answer

Join those two tables and fetch the data, here is an example on how you do it without creating a model.

 $rows = DB::table('food')->select('food.food_id','food.foodName','categories.categoryName')->join('categories','categories.category_id','=','food.category_id')->where('categories.categoryName', '=', 'breakfast')->get();

you can remove the where('','','') part if you dont want to filter

good luck