根据类别(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">{{$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');

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?

You can use belongsToMany() relationship to retrieve food items belonging to specific category.

In your Category Model, add the following method:

public function Foods(){
    return $this->belongsToMany('App\Foods','category_food','Category_id','Food_‌​id');
}

now in your controller you can retriev the foods as below

$category_name = Input::get('category_name'); //assuming you have a category name in request

$category = Category::where('CategoryName',$category_name)->first(); //retrieve category id 

if($category){
    $foods = $category->Foods()->get();
    //your code
}
else(){
//your code to redirect the user when category name is not valid
}

Update:

Implementation:
Change your default index route as below.

Route::get('index/{category_name?}','DetailsController@index');

Now, modify your index() method in DetailsController to handle the category_name, if present.

public function index($category_name=null)
{
      if($category_name==null){ //if no category_name is present return all foods
          $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'));
        }
        else{//if category_name is present return all foods related to that category
            $category = Category::where('CategoryName',$category_name)->first(); //find the category having category name given in $category_name
            if($category==null){ // Check if category exists with the given category_name
                abort(400);
            }
            else { // if category exists
                $Foods = $category->Foods()->get();
                return view('index.welcome', compact('Foods'));
            }
        }
 }

You need to have two models. Food and Category. It should have many to many relationship.

// Food.php
class Food extends Model
{
    public function categories()
    {
        return $this->belongsToMany('App\Category', 'Category_id', 'Food_‌​id');
    }
}


// Category.php
class Category extends Model
{
    public function foods()
    {
        return $this->belongsToMany('App\Food', 'Category_id', 'Food_‌​id');
    }
}

In your controller get the category which you want.

// Controller
// Url could be: example.com?category=breakfast
public function index(Request $request)
{
    $cat = $request->input('category');

    $data['category'] = Category::where('CategoryName', $cat)->first();

    return view('index.welcome', $data);
}

In your view you need to loop the foods.

@foreach($category->foods as $food)
    {{ $food->FoodName }}
@endforeach