Here are the complete answer
- Web.php
Route::get('/faq', 'MainController@faq')->name('faq');
Route::any('/faq-search', 'MainController@faqSearch')->name('faqSearch');
- Controller
public function faq()
{
$faqs = Faq::paginate(5);
return view('guest.faq', compact('faqs'));
}
public function faqSearch(Request $request)
{
$faqSearch = $request->get('faqSearch');
$faqs = Faq::where('en_question', 'like', $faqSearch.'%')->paginate(5)->setPath('');
$pagination = $faqs->appends(array(
'faqSearch' => $faqSearch
));
return view('guest.faq', compact('faqs', 'faqSearch'));
}
- View (Blade)
<form action="{{route('faqSearch')}}" method="post">
@csrf
<div class="input-group flex-nowrap">
<div class="input-group-prepend">
<button class="btn btn-primary input-group-text" type="submit">
<i class="fa fa-search"></i>
</button>
</div>
<input type="text" name="faqSearch" class="form-control" placeholder="Search" aria-label="Search" aria-describedby="addon-wrapping" value="{{ old('faqSearch', $faqSearch ?? '') }}" >
</div>
</form>
<div class="col-md-3 mx-auto">
{{ $faqs->links() }}
</div>
Thank you so much to "Avinash Nethala"
Resource
- https://medium.com/justlaravel/paginated-data-with-search-functionality-in-laravel-ee0b1668b687
- https://laravel.com/docs/5.3/pagination