实现 Searchable,Laravel 的搜索特征
Implementing Searchable, a search trait for Laravel
我正在尝试使用 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,
]];
}
控制器:搜索控制器
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');
只需将包添加到您的 "composer.json" 文件并 "composer update"[更新您的作曲家]
"nicolaslopezj/searchable": "1.*"
我正在尝试使用 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,
]];
}
控制器:搜索控制器
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');
只需将包添加到您的 "composer.json" 文件并 "composer update"[更新您的作曲家] "nicolaslopezj/searchable": "1.*"