У меня небольшая проблема с тем, что результат поиска отображается даже без ключей поиска. вот фрагмент. это вид:
<form action="/search" method="GET">
<div class="form-group search-location">
<input type="text" name="cityKey" id="cityKey" value="{{ request()->input('cityKey') }}"
class="form-control" >
</div>
<div class="form-group search-info">
<input type="text" name="key" id="key" value="{{ request()->input('key') }}"
class="form-control" >
</div>
<button type="submit" class="btn btn-primary search-btn"><i class="fas fa-search"></i>
<span>search</span></button>
Вот контроллер:
public function search(Request $request){
$cityKey = $request->cityKey;
$key = $request->key;
$doctors = Doctor_list::where('speciality_title', 'LIKE', '%' . $key . '%')->
where('location', 'LIKE', '%' . $cityKey . '%')->
orWhere('doctors_name', 'LIKE', '%' . $key . '%')->
where('location', 'LIKE', '%' . $cityKey . '%')->
orWhere('speciality_type', 'LIKE', '%' . $key . '%')->
where('location', 'LIKE', '%' . $cityKey . '%');
// завершаем запрос и завершаем его с помощью paginate или -> get () $ doctor = $ doctor-> get ();
return view('search', compact('doctors'));
}
1 ответ
Решение простое, просто не передавайте переменную $ Doctor для просмотра (или лучше сказать, что pass и empty var), а в поле зрения обнаруживайте ее пустую и говорите, что нет результатов для поиска. вот код:
public function search(Request $request){
$cityKey = $request->cityKey;
$key = $request->key;
if (filled($cityKey) && filled($key)) {
$doctors = Doctor_list::where('speciality_title', 'LIKE', '%' . $key . '%')->
where('location', 'LIKE', '%' . $cityKey . '%')->
orWhere('doctors_name', 'LIKE', '%' . $key . '%')->
where('location', 'LIKE', '%' . $cityKey . '%')->
orWhere('speciality_type', 'LIKE', '%' . $key . '%')->
where('location', 'LIKE', '%' . $cityKey . '%')->
get();
}
return view('search', [
'doctors' => $doctors ?? []
]);
}
С этим вы даже не запрашиваете базу данных. и fill - вспомогательная функция laravel возвращает, не является ли данное значение "пустым", здесь ссылка: https : //laravel.com/docs/helpers#method-filled
Похожие вопросы
Новые вопросы
php
PHP - это широко используемый высокоуровневый, динамический, объектно-ориентированный и интерпретируемый язык сценариев, в первую очередь предназначенный для серверной веб-разработки. Используется для вопросов о языке PHP.