Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mani4545's avatar

Get food items based on Category (Laravel)

I am new to laravel and facing problem.I have 3 tables that is

Food

  • Food_id(pk)
  • FoodName
  • FoodImage....

Categories

  • Category_id(pk)
  • CategoryName

category_food

  • Category_id(fk)
  • Food_id(fk)

My Controller is

 public function index()
{
      $Foods = DB::table('category_food')
        ->select('Food.Food_id','Food.FoodName','Food.FoodType','Food.FoodDescription',
                'Food.FoodImage','Categories.CategoryName')
        ->join('Categories','Categories.Category_id',
                '=','category_food.Category_id')
        ->join('Food','Food.Food_id',
                '=','category_food.Food_id')->get();

    return view('index.welcome', compact('Foods'));

 }

With this query I am able to get all categories and food items in my view as:

@foreach ($Foods as $categories) 
<button 
style="font-size:14px; width: 230px; height: 30px; background-color:   #00A30C; color:white; font-style: italic; text-align: center; font-size: 15px;   margin: 0 auto;">
<a  href="category">{{$categories->CategoryName}} </a></button>
</div>
@endforeach

  @foreach($Foods as $Food)
 <img src="{{ asset('uploads/'.$Food->FoodImage) }}" /><button 
 style="font-size:14px; width: 230px; height: 30px; background-color: #00A30C; color:white; font-style: italic; text-align: center; font-size: 15px; margin: 0 auto;">
 <a  href="index/{{$Food->Food_id}}">{{$Food->FoodName}}
 <i class="fa fa-chevron-circle-right"></i></a></button>
  </div>
  @endforeach   

My routes are:

Route::get('index','DetailsController@index');
Route::get('index/{Food_id}', 'DetailsController@show');

In Food.php

public function categories()
{
    return $this->belongsToMany('App\Categories','category_food','Food_id','Category_id');
}

And in Categories.php

public function food()
{
return $this->belongsToMany('App\Food','category_food','Category_id','Food_id');
}

Now I want to show food items based on category.For example if category is Breakfast then only food items related to this are shown.Or any other Lunch,Dinner etc.How I can do it?

0 likes
6 replies
ohffs's avatar
$category = Categories::with('food')->where('CategoryName', '=', 'Breakfast')->first();

Now $category->food is a collection of all the food records so in a view you could do :

@foreach ($category->food as $food)
  <li>{{ $food->FoodName }}</li>
@endforeach
Mani4545's avatar

@ohffs I have list of categories in my welcome view.Like Breakfast,Lunch,Dinner etc.I want when I click on any category name it shows me food items relevant to that category.

ohffs's avatar

Just make each category name a link to a route that takes the category id and returns the right view. So you end up with a url like '/category/2/foods' or '/category/Breakfast/foods' and then pass that to a method on your controller that does the query a bit like I posted above.

Mani4545's avatar

You mean that I have to make individual view for each category?

ohffs's avatar

Not unless you want them to be very different - that's why you pass the category name/id as a parameter then just use the same view.

Please or to participate in this conversation.