亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

laravel 5.8 不搜索任何東西就回顯

laravel 5.8 不搜索任何東西就回顯

PHP
慕田峪9158850 2023-06-18 16:06:18
我有一個小問題,即使沒有搜索鍵也會顯示搜索結果。這是片段。這是觀點:<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 . '%');//完成查詢并使用分頁或 ->get() 終止查詢 $doctors = $doctors->get();    return view('search', compact('doctors'));}
查看完整描述

1 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

解決方案很簡單,只是不要將 $doctors 變量傳遞給 view(或者更好地說傳遞和清空 var)并在 view 中檢測到它為空并說沒有搜索結果。這是代碼:


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 ?? []

? ? ]);

}

有了這個你甚至不用查詢數據庫。filled 是一個 laravel 輔助函數返回給定值是否不是“空白”鏈接


查看完整回答
反對 回復 2023-06-18
  • 1 回答
  • 0 關注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號