phparraysajaxlaravelmodel

Laravel ajax live search query with multiple models


Currently i am trying to make live search field. When user starts typing, result are coming under the text input field. For just one model its working perfectly. But if i want to search in multiple models can't display any results.

Thats what i have done so far. This is working for one model.

public function autoComplete(Request $request) {
    $query = $request->get('term','');

    $news=News::where('title','LIKE','%'.$query.'%')->get();

    $data=array();
    foreach ($news as $new) {
        $data[]=array('value'=>$new->title,'id'=>$new->id);
    }
    if(count($data))
        return $data;
    else
        return ['value'=>'No Result Found','id'=>''];
}

But this isn't.

public function autoComplete(Request $request) {
    $query = $request->get('term','');

    $news=News::where('title','LIKE','%'.$query.'%')->get();
    $products=Product::where('title', 'LIKE', '%'.$query.'%')->get();
    $searchResults = array_merge($news->toArray(), $products->toArray());

    $data=array();
    foreach ($searchResults as $results) {
        $data[]=array('value'=>$results->title,'id'=>$results->id);
    }
    if(count($data))
        return $data;
    else
        return ['value'=>'No Result Found','id'=>''];
}

Also that didn't worked too.

    $news = News::orderBy('order', 'asc')->get();
    $products= Product::orderBy('order', 'asc')->get();
    $results = array();
    $results['products'] = $sliders;
    $results['news'] = $news;

I'll glad if anyone help. I can't do anything right now. I just want to search in multiple models like searching in just one model.

Also this is my javascript codes.

<script>
$(document).ready(function() {
    src = "{{ route('searchajax') }}";
    $("#search_text").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: src,
                dataType: "json",
                data: {
                    term : request.term
                },
                success: function(data) {
                    response(data);
                    for (var i=0; i<data.length; i++) {
                        console.log(data[i]['value']);}
                }
            });
        },
        minLength: 2,
    });
});

View:

<form action="#">
   <i class="fa fa-search c-gray-1-dark f-27 pl-10px"></i>
   <input type="text" name="search_text"  id="search_text" value="{{ old('title') }}" />
</form>

Solution

  • For merging results, You can try this

    public function autoComplete(Request $request) {
        $query = $request->get('term','');
    
        $news=News::where('title','LIKE','%'.$query.'%')->get();
        $products=Product::where('title', 'LIKE', '%'.$query.'%')->get();
    
        $searchResults = $news->merge($products);
    
        $data=array();
        foreach ($searchResults as $results) {
            $data[]=array('value'=>$results->title,'id'=>$results->id);
        }
        if(count($data))
            return $data;
        else
            return ['value'=>'No Result Found','id'=>''];
    }
    

    Hope this will help you.