/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index($tag_id = null) { if ($tag_id) { $ingredients = Ingredient::whereHas('tags', function ($query) use($tag_id) { $query->where('id', $tag_id); })->get(); } else { $ingredients = Ingredient::all(); } $tags = Tag::where('for', 'ingredient')->get(); $tag = Tag::find($tag_id); return view('ingredients.index')->with(['ingredients' => $ingredients, 'tags' => $tags, 'the_tag' => $tag]); }