monstajamss's avatar

Trying To Paginate (Call to undefined method App\Post::links())

I am trying to do custom pagination but it does not work instead it gives error Controller

public function postindex(User $user)
    {
        $post=Post::where('user_id',$user->id)->latest()->get();
        $post = $user->post()->latest()->paginate(15);
        return view ('author.show', compact('post','user'));
    }

Blade

<?php echo $post->links('cpag.custom'); ?>

Then i get this errorCall to undefined method App\Post::links()

0 likes
2 replies
Tray2's avatar

Change this line

$post = $user->post()->latest()->paginate(15);

To

  $user = $user->post()->latest()->paginate(15);

It overwrites the first one. And then change

<?php echo $post->links('cpag.custom'); ?>

To

<?php echo $user->links('cpag.custom'); ?>
monstajamss's avatar

@tray2 getting this error Undefined property: Illuminate\Pagination\LengthAwarePaginator::$username

<h2>{{$user->username}}</h2>
<span>{{ $user->post()->count() }} items</span>

and also this $user = $user->post()->latest()->paginate(15); is getting the same post for page 2,3 etc

Please or to participate in this conversation.