Answer the question
In order to leave comments, you need to log in
How to solve Laravel pagination and date issue?
Hello. I ran into a display problem in the created_at field template, I needed to display the date the post was created. The date was displayed, but was always the same.
Such a controller
public function index()
{
$posts = Blog::join('users', 'author_id','=', 'users.id')
->orderBy('blogs.created_at', 'desc')
->simplePaginate(15);
return view('posts.blog', compact('posts'));
}
{{$post->created_at->diffForHumans()}}
public function index()
{
$blogs = Blog::join('users', 'author_id','=', 'users.id')
->orderBy('blogs.created_at', 'desc')
->simplePaginate(15);
return view('posts.blog', compact('blogs'));
}
$posts
replaced it with $blogs
{{ $blogs->links() }}
an error occursMethod Illuminate\Database\Eloquent\Collection::links does not exist. (View: W:\domains\blog\resources\views\posts\blog.blade.php)
Answer the question
In order to leave comments, you need to log in
And you look at your query, mb you will notice that it pulls out a bunch of fields with the same names....
I decided so, it seems to work, but somehow it doesn’t turn out beautifully
public function index()
{
$blogs = Blog::join('users', 'author_id','=', 'users.id')
->orderBy('blogs.created_at', 'desc')
->get();
$paginateBlog = Blog::paginate(10);
return view('posts.blog', compact('blogs', 'paginateBlog'));
}
Yes, you read the documentation, you will find how Eloquent works with relationships, then the code will be in one line
return view('posts.blog',
[
'blogs' => Blog::with('user')->orderBy('created_at')->paginate(10)
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question