duanchao1002 2017-08-10 06:21
浏览 159

实现Serachable,Laravel的搜索特性

I was trying to implement a searchable function using Searchable, a search trait for Laravel by nicolaslopezj, i have used the following code. But it doesn't seem to work. If there are only two records in the database it show the records but if more then two records it doesn't search.

Model: 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);
        }
}

View: 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>

Routes:

Route::get('find_contact', 'SearchController@findContact');
Route::get('find_contact_all', 'SearchController@contactPrefetch');
  • 写回答

1条回答 默认 最新

  • drxzpo70788179614 2019-02-08 17:41
    关注

    Simply add the package to your "composer.json" file and "composer update"[update your composer] "nicolaslopezj/searchable": "1.*"

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码