你知道为什么城市名字出现两次吗?

时间:2018-05-22 14:12:01

标签: jquery laravel

我使用自动完成插件进行输入搜索,该插件显示会议名称和将召开会议的城市。当用户键入1个单词时,结果将显示在自动完成中。

但是有一个问题,例如,如果在会议桌中有两个城市列相等的会议,自动完成输入中的示例“纽卡斯尔”出现两次“纽卡斯尔”。但它应该只出现一次。你知道问题出在哪里吗?

AutoCompleteController:

class AutocompleteController extends Controller
{
    public function index(){
    return view('autocomplete.index');
    }

    public function search(Request $request){
        $search = $request->term;
        $conferences = Conference::where('name', 'LIKE', '%'.$search.'%')->get();

        $cities = Conference::where('city', 'LIKE', '%'.$search.'%')->get();


        //dd($cities); 
        $data= [];
        foreach ($conferences as $key => $value){
            $data[] = ['category'=> 'Conferences', 'value' => $value->name, 'url' => 'conference/'.$value->id.'/'.$value->slug];
        }

        foreach ($cities as $key => $value){
            $data[] = ['category'=> 'Cities', 'value' => $value->city, 'url' => 'conferences/where/city/'.$value->city];
        }
        return response($data);
    }
}

自动完成Jquery:

$.widget( "custom.catcomplete", $.ui.autocomplete, {
_create: function() {
    this._super();
    this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
    },
    _renderMenu: function( ul, items ) {
    var that = this,
    currentCategory = "";
    $.each( items, function( index, item ) {
        var li;
        if ( item.category != currentCategory ) {
            ul.append( "<li class='ui-autocomplete-category bg bg-light-gray2 h6 font-weight-bold text-heading-blue'>"
            + item.category + "</li>" );
            currentCategory = item.category;
        }
        li = that._renderItemData( ul, item );
        if ( item.category ) {
        li.attr( "aria-label", item.category + " : " + item.label );
        }
    });
    }
});

$("#search").catcomplete({
    source: "{{ URL::to('autocomplete-search') }}",
    select: function(event, ui) {
    window.location.href = ui.item.url;
}

键入“新建”时,dd($ cities)显示:

Collection {#274
#items: array:2 [
0 => Conference {#275
#fillable: array:18 [
0 => "name"
5 => "city"
...
]
#dates: array:2 [
0 => "start_date"
1 => "end_date"
]
#appends: array:1 [
0 => "price_range"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [
"id" => 1
"name" => "conf1"
"city" => "Newcastle"
]
#original: array:23 [
"id" => 1
"name" => "conf1"
"city" => "Newcastle"
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
1 => Conference {#276
#fillable: array:18 [
0 => "name"
5 => "city"
...
]
#dates: array:2 [
0 => "start_date"
1 => "end_date"
]
#appends: array:1 [
0 => "price_range"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [
"id" => 2
"name" => "conf2"
"city" => "Newcastle"
...
]
#original: array:23 [
"id" => 2
"name" => "conf2"
"city" => "Newcastle"
...
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
]
}

它显示没有dd($ cities):

[{category: "Cities", value: "Newcastle", url: "conferences/where/city/Newcastle"},…]
0
:
{category: "Cities", value: "Newcastle", url: "conferences/where/city/Newcastle"}
1
:
{category: "Cities", value: "Newcastle", url: "conferences/where/city/Newcastle"}

1 个答案:

答案 0 :(得分:1)

你可以像这样使用'distinct()`:

$cities = Conference::where('city', 'LIKE', '%'.$search.'%')-> distinct()->get(['city']);