我试图使用Searchable实现一个可搜索的功能,这是一个由nicolaslopezj为Laravel的搜索特性,我使用了以下代码。但它似乎不起作用。如果数据库中只有两条记录,则显示记录,但如果超过两条记录则不会搜索记录。
型号:Contact.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Nicolaslopezj\Searchable\SearchableTrait;
class Contact extends Model
{
use SearchableTrait;
protected $searchable = [
'columns' => [
'contacts.first_name' => 10,
'contacts.last_name' => 10,
]];
}
Controller:SearchController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Nicolaslopezj\Searchable\SearchableTrait;
use View;
use App\Contact;
use App\Tag;
use App\Project;
use App\User;
//use Illuminate\Support\Facades\Input;
class SearchController extends Controller
{
public function findContact(Request $request)
{
return Contact::search($request->get('cname'))->get();
}
public function contactPrefetch()
{
$all_contacts= Contact::All();
return \Response::json($all_contacts);
}
}
查看:show.blade.php
<script src="{{asset('global/js/plugins/datatables/jquery.dataTables.min.js')}}"></script>
<script src="{{asset('global/js/pages/base_tables_datatables.js')}}"></script>
<div class="input-group input-medium " style="float: right; padding-top: 3px; ">
<input type="search" name="cname" class="form-control search-input" placeholder="search contact" autocomplete="off" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Bootstrap JS -->
<!-- Typeahead.js Bundle -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
<script>
jQuery(document).ready(function($) {
// Set the Options for "Bloodhound" suggestion engine
var engine = new Bloodhound({
prefetch: '/find_contact_all',
remote: {
url: '/find_contact?q=%QUERY%',
wildcard: '%QUERY%'
},
datumTokenizer: Bloodhound.tokenizers.whitespace('cname'),
// queryTokenizer: Bloodhound.tokenizers.whitespace
});
$(".search-input").typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
source: engine.ttAdapter(),
name: 'contact',
display: function(data) {
return data.first_name + ' '+ data.last_name ;
},
templates: {
empty: [
'<a class="list-group-item"> Agent not found.</a>'
],
header: [
'<div class="list-group search-results-dropdown">'
],
suggestion: function (data) {
return '<a href="/home/contact/profile/'+data.id+'" class="list-group-item">' + data.first_name + ' ' + data.first_name + '</a>'
}
}
});
});
</script>
路线:
Route::get('find_contact', 'SearchController@findContact');
Route::get('find_contact_all', 'SearchController@contactPrefetch');
答案 0 :(得分:0)
只需将软件包添加到您的“ composer.json”文件和“ composer update” [更新您的作曲家] “ nicolaslopezj / searchable”:“ 1。*”