laravel 如何用 AJAX 计算总结果?

92dk7w1h  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(124)

有一个问题与 AJAX 搜索的结果总数。有一个错误“方法Illuminate\Database\Eloquent\Collection::total不存在。”如果我使用例如指令

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

如何正确修复?
刀片模板:

@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 代码:

$(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();
                })
            }
        });
    });

控制器:

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

这对搜索结果是正确的,我看到了它们,但我没有修复以查看来自请求的总结果的准确计数。

ykejflvf

ykejflvf1#

集合使用count()方法返回总数。
所以你应该这样使用它

{{ $sfilms->count() }}
ux6nzvsh

ux6nzvsh2#

已完成
只需将控制器中的->limit(4)->get()替换为->paginate(4)。根据文档

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

相关问题