lundi 22 août 2016

Get food items based on Category (Laravel)

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 as:

 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"> </a></button>
</div>
@endforeach

  @foreach($Foods as $Food)
 <img src="" /><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/">
 <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');

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.How I can do it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire