リレーションを持たない複数のテーブルを一括で検索して表示したいです。
例えば、全くの別テーブルであるfruits tableとvegetables tableの検索結果を結合してpaginateして表示することは可能なのでしょうか?
php
1// キーワードを取得 2$keyword = $request->input('keyword'); 3 4// もしキーワードが入力されている場合 5if (!empty($keyword)) { 6 // フルーツテーブルから検索 7 $fruits = DB::table('fruits') 8 ->where('name', 'like', '%'.$keyword.'%'); 9 // 野菜テーブルから検索 10 $vegetables = DB::table('vegetables') 11 ->where('name', 'like', '%'.$keyword.'%'); 12 13 // 検索結果を結合したい 14 $all = collect($fruits)->merge($vegetables)->paginate(7); 15 16// キーワードが入力されていない場合 17} else { 18 $vegetables = DB::table('vegetables'); 19 $fruits = DB::table('fruits'); 20}
上記のコードだと
Method Illuminate\Support\Collection::paginate does not exist.
というエラーになってしまいます。
ご教示いただけると幸いです。
fruits table
id (INT)
title (VARCHAR)
body (TEXT)
created_at (TIMESTAMP)
updated_at (TIMESTAMP)
vegetables table
id (INT)
title (VARCHAR)
body (TEXT)
created_at (TIMESTAMP)
updated_at (TIMESTAMP)
回答2件
あなたの回答
tips
プレビュー