按类别ID获取类别名称,不使用if else语句

时间:2017-10-08 18:16:22

标签: laravel-5

public function shirts($type = '') {
  {
    if ($type == 'glass') {
      $shirt = Product::where('category_id', '1') - > get();
    }
    elseif($type == 'ic') {
      $shirt = Product::where('category_id', '2') - > get();
    }
    elseif($type == 'cover') {
      $shirt = Product::where('category_id', '3') - > get();
    } else {
      $shirt = Product::all();
    }
    $products = Category::find($shirt);
    return view('front.shirt', compact('products', 'shirt'));
  }
}

可以帮助我最小化这个控制器我不想使用if else语句

0 个答案:

没有答案