Neeraj1005's avatar

Method Illuminate\Database\Eloquent\Collection::links does not exist. (View: C:\xampp\htdocs\mylearnwork\codehacking\resources\views\themes\blog\blog_front.blade.php)

Basically in my project I'm trying to fiter the post by using category I am getting the post but pagination is not working. can anyone give the suggestion where did I make mistake? controller code

public function bloglist() {

        $blogs = Post::with('photo','user')->latest();

        if (request('category')) {

            $categoryname = request('category');

            $blogs = $blogs->when($categoryname, function($query, $categoryname) {
                $query->withCategory($categoryname);
            })->paginate(2)->append('category',$categoryname);

        } else {

            $blogs = $blogs->paginate(3);
        }

        return view('themes.blog.blog_front',compact('blogs'));
    }

and this is my blade file links code

<x-blog.app >

    @section('title', 'Page')

    @forelse($blogs as $blog)
    <div class="max-w-sm w-full lg:max-w-full lg:flex my-1">
        <div class="h-48 lg:h-auto lg:w-48 flex-none bg-cover rounded-t lg:rounded-t-none lg:rounded-l text-center overflow-hidden"
        style="background-image: url('{{ $blog->photo->file }}')" title="Woman holding a mug">
        </div>
        <div class="border-r border-b border-l border-gray-400 lg:border-l-0 lg:border-t lg:border-gray-400 bg-white rounded-b lg:rounded-b-none lg:rounded-r p-4 flex flex-col justify-between leading-normal">
          <div class="mb-8">
            <div class="text-gray-900 font-bold text-xl mb-2">{{ $blog->title ?? '' }}</div>
            <p class="text-gray-700 text-base">
                {{ $blog->shortDescription() ?? '' }}
            </p>
          </div>
          <div class="flex items-center">
            <img class="w-10 h-10 rounded-full mr-4" src="{{url('images/jonathan.jpg')}}" alt="Avatar of Jonathan Reinink">
            <div class="text-sm">
              <p class="text-gray-900 leading-none">{{ $blog->user->name }}</p>
              <p class="text-gray-600">{{ $blog->created_at->isoFormat('MMM YYYY') }}</p>
            </div>
          </div>
        </div>
    </div>
    @empty
        {{('No blogs Available')}}
    @endforelse
    {{ $blogs->links('vendor.pagination.tailwind') }}

</x-blog.app>

0 likes
8 replies
bobbybouwmann's avatar

Mmhm, this actually does look correct.

Can you do a dd in your controller on the $blogs variable to make sure it's actually a class with the type LengthAwarePaginator? If this is the case, this should work.

2 likes
Neeraj1005's avatar

@bobbybouwmann Result are:

Illuminate\Database\Eloquent\Collection {#1411 ▼
  #items: array:2 [▼
    0 => App\Post {#1399 ▶}
    1 => App\Post {#1403 ▶}
  ]
}

Is my if condition is correct for pagination?

MichalOravec's avatar
Level 75

In controller

use Illuminate\Http\Request;

public function bloglist(Request $request)
{
    $blogs = Post::with(['photo', 'user'])->when($request->category, function($query, $category) {
        $query->withCategory($category);
    })->latest()->paginate($request->has('category') ? 2 : 3);

    return view('themes.blog.blog_front', compact('blogs'));
}

In view but put it inside forelse statement, above @empty

{{ $blogs->appends(request()->has('category') ? ['category' => request('category')] : [])->links('vendor.pagination.tailwind') }}

// or

{{ $blogs->withQueryString()->links('vendor.pagination.tailwind') }}
1 like
Neeraj1005's avatar

@michaloravec This error comes from blade file

{{ $blogs->appends($request->has('category') ? ['category' => request('category')] : [])->links('vendor.pagination.tailwind') }} // or {{ $blogs->withQueryString()->links('vendor.pagination.tailwind') }}

withQueryString is working but $blog->append gives an error Undefined variable: request

Neeraj1005's avatar

@michaloravec Now both working could you please explain between what the difference between here these two line? withQueryString or append

Please or to participate in this conversation.