public function favall(Request $request, $id) { $user = User::find($id, ['id', 'name']); if ($request->input('sort') == 'rating') { $sort = 'rating'; } else { $sort = 'updated_at'; } if ($request->input('tag')) { $tag = Tag::where('name', $request->input('tag'))->first(); $favorites = Favorite::join('tagmaps', function ($join) use($tag, $user) { $join->on('favorites.user_id', '=', 'tagmaps.user_id')->on('favorites.drama_id', '=', 'tagmaps.drama_id')->where('favorites.user_id', '=', $user->id)->where('tagmaps.tag_id', '=', $tag->id); })->select('favorites.*')->orderBy($sort, 'desc')->paginate(20); $favorites->load('drama'); $tag = $tag->name; } else { $tag = null; $favorites = Favorite::with('drama')->where('user_id', $id)->orderBy($sort, 'desc')->paginate(20); } $favorites->load(['reviews' => function ($query) use($id) { $query->where('user_id', $id); }]); return view('user.favall', ['user' => $user, 'favorites' => $favorites, 'sort' => $sort, 'tag' => $tag]); }