Home > Net >  How to count total results with ajax?
How to count total results with ajax?

Time:11-11

Have a problem with total count of ajax searche's results. There is a mistake "Method Illuminate\Database\Eloquent\Collection::total does not exist." if I use directive for example

<div >
            <a href="#" >{{ __('main.res_found') }} {{$sfilms->total()}} {{ __('main.res_results') }}</a>
        </div>

How to fix it correctly?

blade template:

@if($sfilms)
    @if($sfilms->count())
        <div >
            <a href="#" >{{ __('main.res_found') }} {{$sfilms->count()}} {{ __('main.res_results') }}</a>
        </div>
    @else
        <div >
            <a href="#" >{{ __('main.res_found') }} 0 {{ __('main.res_results') }}</a>
        </div>
    @endif
    @foreach($sfilms as $sfilm)
    <div >
        <ol >
            <li >
                <a href="{{ url('films/'.$sfilm->film_id) }}">
                    <div > 
                        @if(!is_null($sfilm->films->poster))
                            <img src="{{$sfilm->films->poster}}" alt="poster" >
                        @else
        {{--                <img  src="{{ url('/img/no_poster.jpg') }}" alt="poster"/>--}}
                            <img src="{{asset('storage/poster/' . $sfilm->films->id . '.jpg')}}" alt="poster" >
                        @endif
                    </div>
                    <div >
                        <div >
                            <h3>{{ $sfilm->title }}</h3>
                        </div>
                        <div >
                            <h4>{{ $sfilm->films->orig_title }}</h3>
                        </div>
                        <span>
                            <p >{{ $sfilm->films->country}} {{ $sfilm->films->year }}</p>
                        </span>
                        <span>
                            <p >For age {{ $sfilm->films->age }}  </p>
                        </span>
                    </div>
                </a>
            </li>
        </ol>
    </div>
    @endforeach
@else
    <li >{{ __('search_no_results') }}</li>
@endif

javascript ajax code:

$(document).ready(function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $('#search').keyup(function() {
            var search = $('#search').val();
            if (search == "") {
                $("#memlist").html("");
                $('#result').hide();
            } else {
                $.get("{{ URL::to('/search') }}", {
                    search: search
                }, function(data) {
                    $('#memlist').empty().html(data);
                    $('#result').show();
                })
            }
        });
    });

Controller:

public function search(Request $request) : View
    {
        $search = $request->input('search');
        $locales = app()->getLocale();

        if ($locales == NULL) {
            $sfilms = Local::where('title_en', 'like', "%$search%")
        ->orWhere('year', 'like', "$search%")->limit(4)->get();
        } else {
            $sfilms = Local::where('title' . '_' . $locales, 'like', "%$search%")
        ->orWhere('year', 'like', "$search%")->limit(4)->get();
        }
  
        return view('layouts.found')->with('sfilms', $sfilms);
    }

This works correctly with searched results and I see them, but I don't fix to see exactly count of total results from request.

CodePudding user response:

Collection use the count() method to return the total.

So you should be using it like this instead

{{ $sfilms->count() }}

CodePudding user response:

Done

Just replace in controller ->limit(4)->get() to ->paginate(4). According to documentation https://laravel.com/docs/9.x/pagination#paginator-instance-methods

public function search(Request $request) : View
    {
        $search = $request->input('search');
        $locales = app()->getLocale();

        if ($locales == NULL) {
            $sfilms = Local::where('title_en', 'like', "%$search%")
        ->orWhere('year', 'like', "$search%")->paginate(4); 
        } else {
            $sfilms = Local::where('title' . '_' . $locales, 'like', "%$search%")
        ->orWhere('year', 'like', "$search%")->paginate(4);
        }  
  
        return view('layouts.found', ['sfilms'=>$sfilms]);
    }
  • Related