skip to Main Content

Have a problem with total count of ajax searche’s results.
There is a mistake "Method IlluminateDatabaseEloquentCollection::total does not exist." if I use directive for example

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

How to fix it correctly?

blade template:

@if($sfilms)
    @if($sfilms->count())
        <div class="searched-item">
            <a href="#" class="searched-item-res1">{{ __('main.res_found') }} {{$sfilms->count()}} {{ __('main.res_results') }}</a>
        </div>
    @else
        <div class="searched-item">
            <a href="#" class="searched-item-res2">{{ __('main.res_found') }} 0 {{ __('main.res_results') }}</a>
        </div>
    @endif
    @foreach($sfilms as $sfilm)
    <div class="search-hits">
        <ol class="search-hits-list">
            <li class="search-hits-item">
                <a href="{{ url('films/'.$sfilm->film_id) }}">
                    <div class="search-img-wrapper"> 
                        @if(!is_null($sfilm->films->poster))
                            <img src="{{$sfilm->films->poster}}" alt="poster" class="news_movies-img">
                        @else
        {{--                <img class="news_movies-img" src="{{ url('/img/no_poster.jpg') }}" alt="poster"/>--}}
                            <img src="{{asset('storage/poster/' . $sfilm->films->id . '.jpg')}}" alt="poster" class="news_movies-img">
                        @endif
                    </div>
                    <div class="search-hits-wrapper">
                        <div class="hit-title">
                            <h3>{{ $sfilm->title }}</h3>
                        </div>
                        <div class="hit-title">
                            <h4>{{ $sfilm->films->orig_title }}</h3>
                        </div>
                        <span>
                            <p class="poster_genre-link">{{ $sfilm->films->country}} {{ $sfilm->films->year }}</p>
                        </span>
                        <span>
                            <p class="poster_genre-link">For age {{ $sfilm->films->age }} +</p>
                        </span>
                    </div>
                </a>
            </li>
        </ol>
    </div>
    @endforeach
@else
    <li class="list-group-item">{{ __('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.

2

Answers


  1. Chosen as BEST ANSWER

    Done

    Just replace in controller ->limit(4)->get() to ->paginate(4). According to documentation

    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]);
        }
    

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

    So you should be using it like this instead

    {{ $sfilms->count() }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search