Laravel 使用 Tailwind CSS 在 Blade 文件中分页不起作用

Laravel Pagination not working in Blade file with Tailwind CSS

我正在尝试使用 Laravel 分页。问题是当我在 Blade 文件中使用 links() 方法时,它会出错。例如,当我使用 URL“http://127.0.0.1:8000/tournaments?page=3”时,它工作正常,但它在 Blade 文件中给出了一个错误,如前所述下面。

控制器

public function index()
{
    return view('front.tournaments', [
        'seriesdata' => $this->tournamentList()
    ]);
}

public function tournamentList()
{
    return Series::leftJoin('team_squads as ts', 'ts.id_series', 'series.id')
        ->leftJoin('series_team_squads as sts', 'sts.id_series', 'series.id')
        ->leftJoin('admins as a', 'a.adminable_id', 'series.id')
        ->where('series.lang', 'en')
        ->orderBy('series.id', 'asc')
        ->groupBy('series.id')
        ->select('series.*')
        ->paginate(10)
        ->append([
            'logo_url',
            'location'
        ]);
}

Blade

@foreach($seriesdata as $series)
<div class="flex flex-inline xs:flex-col sm:flex-row">
    <div class="w-full border-b">
        <div class="flex justify-center items-start">
            <div class="py-2 mx-auto sm:bg-white xs:bg-white  w-full">
                <a href="{{ url('tournaments/').'/'.$series->url.'/'.$series->id }}" class="flex">
                    <div class="grid grid-rows-1 grid-flow-col gap-1">
                        <div class="row-span-1 col-span-2">
                            <img src="{{ $series->logo_url }}" alt="avatar" class="object-cover w-12 h-12 mx-4">
                        </div>
                        <div class="row-span-1 col-span-2">
                            <h1 class="font-bold text-lg">{{ $series->name }}</h1>
                            <p class="uppercase font-light text-sm text-grey">{{ $series->location->address }}</p>
                        </div>
                    </div>
                </a>
            </div>
        </div>
    </div>
</div>
@endforeach
{{ $seriesdata->links() }}

错误

您的 append() 方法似乎阻止了 blade 中的分页。尝试将其从控制器中的 tournamentList() 中删除。


当您在 ->paginate() 之后执行 ->append() 时,您正在将 ->paginate() returns(基本上是 LenghtAwarePaginator)转换为 Collection,并且 Collection 没有 ->links() 方法。您可以看到这一点,因为错误显示您正在尝试调用 Illuminate\Database\Eloquent\Collection 中的 links 方法,这就是我知道 ->append() 返回的内容的方式。