【Laravel5.8】検索機能を作成したい
本のタイトル・著者・出版社をキーワード検索できる機能を作ります。
routing
Route::post('/serch', 'CustomerController@serch');
Controller
public function serch(Request $request){ $keyword = $request->input('keyword'); //orWhereでor検索できる。 $books = Book::where('title', 'like', '%'.$keyword.'%')->orWhere('author','like','%'.$keyword.'%')->orWhere('publisher','like','%'.$keyword.'%')->get(); return view ('customer.serch_resault',compact('books','keyword','user_id')); }
view
//検索窓 <form class="uk-search uk-search-default" action="serch" method="post"> {{ csrf_field() }} <span uk-search-icon></span> <input class="uk-search-input" type="search" placeholder="本の検索" name="keyword"> </form>
//検索結果の展開 @foreach($books as $val) <tr> <td><img src="{{$val->photo_path}}" width="50" height="50"></td> <td>{{$val->title}}</td> <td>{{$val->author}}</td> <td>{{$val->price}}円</td> <td><a href="/book/{{$val->id}}">詳細</a></td> </tr> @endforeach