根据类别(Laravel)获取食物

时间:2016-08-23 05:58:58

标签: php laravel-5 laravel-5.1

我有3张桌子

食物:

  • Food_id(PK)
  • FoodName
  • FoodImage ....

分类

  • CATEGORY_ID(PK)
  • 类别名称

category_food

  • CATEGORY_ID(FK)
  • Food_id(FK)

我的控制器如下:

 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'));

 }

通过此查询,我可以在我的视图中获取所有类别和食物项目:

@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   

我的路线是:

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

现在我想根据类别显示食品。例如,如果类别是早餐,那么只显示与此相关的食品。我怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用belongsToMany()关系来检索属于特定类别的食品。

在您的类别模型中,添加以下方法:

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

现在在您的控制器中,您可以检索以下食物

$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
}

更新

实施:
更改您的默认index路线,如下所示。

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

现在,修改index()中的DetailsController方法以处理category_name(如果有)。

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'));
            }
        }
 }

答案 1 :(得分:0)

您需要有两个型号。 >>> sorted(a, key=lambda x: (-x[0], x[1])) [(2, 'c'), (1, 'a'), (1, 'b')] Food。它应该有多对多的关系。

Category

在您的控制器中获取您想要的类别。

// 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');
    }
}

在你看来,你需要循环食物。

// 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);
}