我正在搜索該網站,以便用戶可以從資料庫中搜索結果
一切正常,但是在用戶完成搜索之前我如何隱藏結果?
這是控制器:
class SearchController extends Controller
{
public function search(Request $request){
// Get the search value from the request
$search = $request->input('search');
// Search in the title and body columns from the posts table
$classes = Classes::query()
->where('course_name', 'LIKE', "%{$search}%")
->orWhere('course_lecturer', 'LIKE', "%{$search}%")
->get();
// Return the search view with the results compacted
return view('search', compact('classes'));
}
}
這是視圖:
<div class="container">
<h1>Search</h1>
<form action="{{ route('search') }}" method="GET">
<input style="font-family: Frutiger" dir="rtl" type="text" placeholder="search" name="search" required/>
<button style="font-family: Frutiger" class="col-md-12 btn btn-primary" type="submit">Search</button>
</form>
</div>
@if($classes->isNotEmpty())
@foreach ($classes as $classes)
<div class="post-list text-center">
<p>{{ $classes->course_name }}</p>
<p>{{ $classes->course_lecturer }}</p>
<a target="_blank" href="{{ $classes->file_link }}"><img src="{{ $classes->file_link }}" href="{{ $classes->file_link }}" width="150px" height="300px"></a>
<hr>
</div>
@endforeach
@else
<div>
<h2 class="text-center">No posts found</h2>
</div>
@endif
所以我的問題是視圖在用戶搜索之前顯示所有結果,我希望在搜索完成之前隱藏結果
如何做到這一點?
uj5u.com熱心網友回復:
這是因為在第一次加載頁面時search為空,所以您回傳所有資料庫!
你可以像這樣修復它:
class SearchController extends Controller
{
public function search(Request $request){
// Get the search value from the request
$search = $request->input('search');
$classes = [];
if($search) {
// Search in the title and body columns from the posts table
$classes = Classes::query()
->where('course_name', 'LIKE', "%{$search}%")
->orWhere('course_lecturer', 'LIKE', "%{$search}%")
->get();
}
// Return the search view with the results compacted
return view('search', compact('classes'));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408106.html
標籤:
上一篇:查看未找到例外-laravel
下一篇:我收到兩次訊息,重復
