質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

解決済

1回答

2701閲覧

laravel5.8 商品一覧とその検索フォームのデータの取得について

neginattofan

総合スコア66

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

0クリップ

投稿2020/05/15 16:14

編集2020/05/15 22:16

前提・実現したいこと

商品一覧画面に、検索機能を追加しているのですが、laravelに精通していないため、詰まってしまいました。

indexメソッドでは商品一覧画面で使う商品一覧($productInfo)と、検索機能で使うセレクトボックスのoptionを入れている$categoriesと$prefecturesを返しています。

controller

1 public function index() 2 { 3 $productInfo = Product::where('products.delete_flg', '0') 4 ->select('products.name as product_name', 5 'companies.name as company_name', 6 'prefectures.name as prefecture_name', 7 'products.id as product_id', 8 'orders.user_id as user_id', 9 'pic1', 10 'year', 11 'month', 12 'day', 13 'store', 14 'discount', 15 'price', 16 'sold_flg') 17 ->leftjoin('companies', 'companies.id', '=', 'products.company_id') 18 ->leftjoin('prefectures', 'prefectures.id', '=', 'companies.prefecture_id') 19 ->leftjoin('orders', 'orders.product_id', '=', 'products.id')->orderBy('product_id', 'desc')->get(); 20 $categories = Category::all(); 21 $prefectures = Prefecture::all(); 22 23 24 25 26 return view('/products/index', compact('productInfo', 'categories', 'prefectures') ); 27 }

検索機能を初めて作ったため、indexメソッドで行ったような複数のtableにinnerjoinする方法がわかりません。

こちらを参考にしました。
https://qiita.com/banku/items/4ac99f9a48d2fc862255

検索機能は
prefecture_id
category_id
price_bottom
price_top
expiration

の五つの項目で検索しています。

controller

1 } 2 public function search(SearchRequest $request){ 3 4 $query = Product::query(); 5 6 $prefecture_id = $request->prefecture_id; 7 $category_id = $request->category_id; 8 $price_bottom = $request->price_bottom; 9 $price_top = $request->price_top; 10 $expiration = $request->expiration; 11 12 if ($request->has('price_bottom') && $price_bottom != null && $request->has('price_top') && $price_top != null ){ 13 $query->whereBetween('discount', [$price_bottom, $price_top])->get(); 14 } 15 16 if ($expiration == null ){ 17 $date = Carbon::now()->format('Y-m-d'); 18 $query->where('expiration', '>', $date)->get(); 19 } 20 21 if ($request->has('prefecture_id') && $prefecture_id != null){ 22 if ($query->company()->where('prefecture_id', $prefecture_id)->get() == true) 23 $query->where('prefecture_id', $prefecture_id)->get(); 24 } 25 26 if ($request->has('category_id') && $category_id != null){ 27 $query->where('category_id', $category_id)->get(); 28 } 29 30 $productInfo = $query->get();//ここで検索結果を取ってますが、ここではproductsのtableの情報しかない。どうすれば、index()のように他のテーブル情報も取ってこれるのかが分かりません。 31 $categories = Category::all(); 32 $prefectures = Prefecture::all(); 33 34 35 return view('/products/index', compact('productInfo', 'categories', 'prefectures')); 36 }

初めてlaravelで検索機能を作るのと、eloquentやquerybuilderのこともまだよく分かっていません。

どのようにすればいいのか、よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2020/05/15 21:34 編集

>どのように設計すればいいのか 設計はしてから作るのでは。 eloquentやquerybuilderを使う工程は「製造」ですよ。あくまで手段です。 設計ならLaravel関係なくできます。
neginattofan

2020/05/15 22:16

ありがとうございます。修正します。
m.ts10806

2020/05/15 22:18

ちなみに設計はされたんですよね? あと、SQL自体は作ったのでしょうか? そのSQLを出された方がLaravelでどう書くかのアドバイスもしやすいのですけど。
guest

回答1

0

自己解決

when を使ったらできました。

$query = Product::query(); $prefecture_id = $request->prefecture_id; $category_id = $request->category_id; $price_bottom = $request->price_bottom; $price_top = $request->price_top; $expiration = $request->expiration; $sold = $request->sold; $date = Carbon::now()->format('Y-m-d'); $productList = Product::where('products.delete_flg', '0') ->select('products.name as product_name', 'companies.name as company_name', 'prefectures.name as prefecture_name', 'products.id as product_id', 'orders.user_id as user_id', 'pic1', 'year', 'month', 'day', 'expiration', 'store', 'discount', 'price', 'sold_flg') ->leftjoin('companies', 'companies.id', '=', 'products.company_id') ->leftjoin('prefectures', 'prefectures.id', '=', 'companies.prefecture_id') ->leftjoin('orders', 'orders.product_id', '=', 'products.id') ->orderBy('product_id', 'desc') ->when($category_id, function($query) use ($category_id){ return $query->where('category_id', $category_id); }) ->when(!$expiration, function($query) use ($date){ return $query->whereDate('expiration', '>', $date); }) ->when($price_bottom && $price_top, function($query) use ($price_top, $price_bottom){ return $query->whereBetween('discount', [$price_bottom, $price_top]); }) ->when(!$sold, function($query) use ($sold){ return $query->where('sold_flg', '0'); }) ->when($prefecture_id, function($query) use ($prefecture_id){ return $query->where('prefecture_id', $prefecture_id); }); $productList = $productList->paginate(10); $categories = Category::all(); $prefectures = Prefecture::all(); return view('/products/index', compact('productList', 'categories', 'prefectures'));

投稿2020/05/19 08:20

neginattofan

総合スコア66

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問