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?