人気ランキングサイトを作成しています。
検索機能は、実装出来ました。
しかしエラーも出ず、検索も出来ていますが、
順位(rank)だけが引き継がれず、全員1位になってしまいます。(他のデータは、ちゃんと反映される。)
どのようにすれば、引き継がれるのか教えて頂きたいと思い質問しました。
よろしくお願いします。
FighterController <?php namespace App\Http\Controllers; use Illuminate\Http\Request; //Fighterモデルをインポート use App\Fighter; //Likeモデルをインポート use App\Like; (省略) // DBをインポート use DB; class FighterController extends Controller { //トップページ public function index(Request $request) { //いいねのカウント、いいねが多い順に並び替え、順位を付ける $fighters_date = Fighter::query() ->selectRaw('fighters.*') ->addSelect(DB::raw('count(likes.user_id) as likes_count')) ->addSelect(DB::raw('rank() over (order by count(likes.user_id) desc) as rank')) ->leftJoin('likes', 'fighters.id', '=', 'likes.fighter_id') ->groupBy('fighters.id') ->orderByRaw('count(likes.user_id) desc'); //検索機能 $keyword = $request->input('keyword'); //ユーザーが検索したら if(!empty($keyword)) { // 選手名を検索 $fighters_date->where('fighter_name', 'LIKE', "%{$keyword}%"); } $fighters = $fighters_date->get(); return view('fighters.index',[ 'title' => '格闘家人気ランキング', 'fighters' => $fighters, 'keyword' => $keyword, ]); }
index.blade.php (省略) <form method = "GET" action = "{{ route('fighters.index') }}"> <input type = "text" name = "keyword" value = "{{ $keyword }}" placeholder = "選手名検索"> <input type = "submit" value = "検索"> </form> (省略)
検索後の$fightersの中身 index.blade.php Illuminate\Database\Eloquent\Collection {#1230 ▼ #items: array:1 [▼ 0 => App\Fighter {#1238 ▼ #fillable: array:4 [▶] #connection: "mysql" #table: "fighters" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:9 [▼ "id" => 5 "fighter_name" => "那須川 天心" "image" => "photos/Xv0gk3zVPCbU341krqEdKZm5F3OOs4daTXP1AkTh.jpg" "image_quote" => "RISE公式サイトより引用" "account" => "https://twitter.com/TeppenTenshin" "created_at" => "2022-06-26 16:22:51" "updated_at" => "2022-06-26 16:22:51" "likes_count" => 0 "rank" => 1 ] #original: array:9 [▶] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #guarded: array:1 [▶] } ] }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/30 08:29
2022/06/30 09:10 編集
2022/06/30 12:19
2022/06/30 13:41
2022/06/30 13:54
2022/06/30 14:40