ご覧いただきありがとうございます。
laravel 8.83.6で検索機能を実装しています
検索画面でチェックボックスにチェックが入っていた場合
検索用のSQLの検索条件に追加したいです。
検索画面(home.blade.php)
検索条件にはチェックボックスでないものもあり、入力があったもののみ
検索条件として使用する想定です
html
1 <form method="post" action="{{url('/search')}}"> 2 @csrf 3 {{method_field('get')}} 4 <div class="form-group"> 5 <label>目的で検索</label> 6 <div class="custom-control custom-checkbox"> 7 <label> 8 <input type="checkbox" class="custom-control-input" name="use[]" value="party">宴会 9 </label> 10 </div> 11 <div class="custom-control custom-checkbox"> 12 <label> 13 <input type="checkbox" class="custom-control-input" name="use[]" value="small">少人数 14 </label> 15 </div> 16 <div class="custom-control custom-checkbox"> 17 <label> 18 <input type="checkbox" class="custom-control-input" name="use[]" value="girls">女子会 19 </label> 20 </div> 21 </div> 22 <div class="form-group"> 23 <label>食べたいもので検索</label> 24 <div class="custom-control custom-checkbox"> 25 <label> 26 <input type="checkbox" class="custom-control-input" name="food[]" value="seafood">海鮮 27 </label> 28 </div> 29 <div class="custom-control custom-checkbox"> 30 <label> 31 <input type="checkbox" class="custom-control-input" name="food[]" value="brandcow">銘柄牛 32 </label> 33 </div> 34 <div class="custom-control custom-checkbox"> 35 <label> 36 <input type="checkbox" class="custom-control-input" name="food[]" value="localsake">地酒 37 </label> 38 </div> 39 <div class="custom-control custom-checkbox"> 40 <label> 41 <input type="checkbox" class="custom-control-input" name="food[]" value="craftbeer">クラフトビール 42 </label> 43 </div> 44 </div> 45 46 <div class="text-center"> 47 <button type="submit" class="btn btn-primary col-md-5">検索</button> 48 </div> 49 50 </form> 51
Controllerの検索メソッド
PHP
1 public function search(Request $request){ 2 $keyword_name = $request->shop_name; 3 $keyword_station = $request->nearest_station; 4 $keyword_budget = $request->budget; 5 $keyword_equality = $request->equality; 6 Log::debug(dump($request)); 7 8 $query = Shop::query(); 9 10 11 DB::enableQueryLog(); 12 13 $shops = $query->get(); 14 15 Log::debug(DB::getQueryLog()); 16 17 return view('show',[ 18 'shops'=>$shops 19 ]); 20
しかし、チェックボックスにチェックを入れて検索ボタンを押下した場合でも
controllerでrequestの内容を取得した際にチェックボックスの項目が入っていません
+request: Symfony\Component\HttpFoundation\InputBag {#44 ▼ #parameters: array:6 [▼ "_token" => "3I5XKZllAwGMkCBRfxyA8OF63ZsGQaORqXIoEHz5" "_method" => "get" "shop_name" => null "nearest_station" => null "budget" => null "equality" => "0" ] }
何か記述や設定が足りていないのでしょうか?
※2件の回答をいただいたのでそれに基づいてhome.blade.phpを修正しました
requestの内容は修正後も変わっていません
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/25 14:10