IN2SITES's avatar

Method Illuminate\Database\Eloquent\Collection::links does not exist.

I have a blade page with this code: {{$post->user_id ? $post->user->name : 'no name'}}

This is my route in web.php Route::get('authors/{author:name}',function(User $author){ return view('admin.posts.index',['posts'=>$author->posts]); })->name('authors');

This is my controller:

public function index()
{
    $posts = Post::with(['categories', 'user', 'photo'])->filter()->paginate(20);

    return view('admin.posts.index', compact('posts'));
}

Now when i remove the pagination the posts are filtered by author. But with the pagination not.

Any help appreciated.

0 likes
4 replies
IN2SITES's avatar
IN2SITES
OP
Best Answer
Level 6

got it:

Route::get('authors/{author:name}',function(User $author){ $posts = $author->posts()->paginate(20); return view('admin.posts.index',['posts'=>$posts]); })->name('authors');

the paginator is added in my route so it's no longer a collection.

tykus's avatar

I don't understand what your code is showing us here; you are not delegating to the Controller action; you are serving the Request from the Route Closure; and that Eloquent Query doesn't paginate the posts relation:

Route::get('authors/{author:name}',function(User $author){
    return view('admin.posts.index',['posts'=>$author->posts]);
})->name('authors');
IN2SITES's avatar

If i write it like you say, then it gives me en error.

This is my controller: public function index() { $posts = Post::with(['categories', 'user', 'photo'])->filter()->paginate(20);

    return view('admin.posts.index', compact('posts'));
}

This my blade page:

 <td>
                    <a href="{{route('authors',$post->user->name)}}">
                     {{$post->user_id ? $post->user->name : 'no name'}}
                    </a>
                </td>

So as you can see it's inside a table.

When i use the pagination like this: {{$posts->links()}}

It gives me an error: Method Illuminate\Database\Eloquent\Collection::links does not exist.

But when i add the pagination inside the route, it's resolved:

Route::get('authors/{author:name}',function(User $author){ $posts = $author->posts()->paginate(20); return view('admin.posts.index',['posts'=>$posts]); })->name('authors');

tykus's avatar

@IN2SITES I was simply formatting your original code to point out that there was no pagination there; I didn't modify anything! Anyway it seems you realized this fact yourself.

Also, I don't understand what circumstance you might have the $post->user->name in the route before you actually check for the $post->user_id:

<td>
  @if ($post->user)
    <a href="{{route('authors',$post->user->name)}}">
        {{ $post->user->name }}
    </a>
  @endif
</td>

Please or to participate in this conversation.