0

I'm trying to use Laravel pagination. The issue is that when I use the links() method in the Blade file, it gives an error. For example, when I use the URL "http://127.0.0.1:8000/tournaments?page=3," it works fine, but it gives an error in the Blade file, as explained below.

Controller

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

Error enter image description here

1
  • Always return ->paginate(xxxx) and never add anything else after that method call. ->paginate should be the last thing you call and return ALWAYS, so if you want to add anything else, do it before it. Commented Jul 30, 2021 at 4:28

1 Answer 1

1

It looks like your append() method is blocking pagination in blade. Try to remove it from tournamentList() in controller.


When you do ->append() after the ->paginate(), you are transforming what ->paginate() returns (basically a LenghtAwarePaginator) into a Collection, and Collection does not have a ->links() method. You can see this as the error shows you are trying to call links method in Illuminate\Database\Eloquent\Collection, that is how I know what ->append() is returning.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.