Laravelの検索機能の実装方法について
1.BookControllerに下記を追加する
public function index(Request $request)
{
$input = $request->all();
$books = Book::search($input)->orderBy('id', 'desc')->paginate(10);
$publications = Book::select('publication')->groupBy('publication')->pluck('publication');
$authors = Book::select('author')->groupBy('author')->pluck('author');
return view(
'book.index',
['books' => $books,
// selectboxの値
'publications' => $publications,
'authors' => $authors,
// 検索する値
'name' => $input['name'] ?? '',
'publication' => $input['publication'] ?? '',
'author' => $input['author'] ?? '',
'status' => $input['status'] ?? '',
'note' => $input['note'] ?? '',
]
);
}
2.Models/Book.phpにscopeを追加する。
$input = $request->only('name', 'status', 'author', 'publication', 'note');
public function scopeSearch($query, $search)
{
$name = $search['name'] ?? '';
$status = $search['status'] ?? '';
$author = $search['author'] ?? '';
$publication = $search['publication'] ?? '';
$note = $search['note'] ?? '';
$query->when($name, function ($query, $name) {
$query->where('name', 'like', "%$name%");
});
$query->when($publication, function ($query, $publication) {
$query->where('publication', $publication);
});
$query->when($note, function ($query, $note) {
$query->where('note', 'like', "%$note%");
});
$query->when($status, function ($query, $status) {
$query->where('status', $status);
});
return $query;
}
尚、scopeはグローバルスコープとローカルスコープがあります。
グローバルは、モデルを利用する際に毎回呼び出されるスコープです。
ローカルスコープは、利用したいときに都度呼び出すスコープです。
scopeを呼び出す際は、scopeという文字は入力せず、モデルにscope移行のメソッド名を入力します。
Book::search($input);
の用な感じです。
3.book/index.bladeの12行目にコピペします。
<div class="pt-8">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<div class="m-5">
<form action="{{ route('book') }}">
<div class="flex flex-row">
<div class="col-span-6 sm:col-span-3 p-2 w-48">
<label for="name" class="block text-sm font-medium text-gray-700">本の名前</label>
<input type="text" name="name" id="name" value="{{ $name }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md">
</div>
<div class="p-2 w-48">
<label for="author" class="block text-sm font-medium text-gray-700">著者</label>
<select id="author" name="author" class="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
<option value="">--</option>
@foreach($authors as $option)
<option value="{{ $option }}" @if($author == $option) selected @endif >{{ $option }}</option>
@endforeach
</select>
</div>
<div class="p-2 w-48">
<label for="publication" class="block text-sm font-medium text-gray-700">出版</label>
<select id="publication" name="publication" class="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
<option value="">--</option>
@foreach($publications as $option)
<option value="{{ $option }}" @if($publication == $option) selected @endif >{{ $option }}</option>
@endforeach
</select>
</div>
<div class="col-span-6 sm:col-span-3 p-2 w-48">
<label for="note" class="block text-sm font-medium text-gray-700">特記事項</label>
<input type="text" name="note" id="note" value="{{ $note }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md">
</div>
<div class="col-span-6 sm:col-span-3 p-2 w-48 relative">
<button type="submit" class="absolute inset-x-0 bottom-2 mr-2 shadow bg-blue-500 hover:bg-blue-400 focus:shadow-outline focus:outline-none text-white font-bold py-2 px-4 rounded">検索</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>