I'm developing a blog application in Laravel 8.
ArticlesController Controller I have this method to display a single article and its Comments:
class ArticlesController extends FrontendController { // More code public function show($slug) { // Single article $article = Article::firstWhere('slug', $slug); $old_article = Article::where('id', '<', $article->id)->orderBy('id', 'DESC')->first(); $new_article = Article::where('id', '>', $article->id)->orderBy('id', 'ASC')->first(); // Comments $commentsQuery = Comment::where(['article_id' => $article->id, 'approved' => 1])->orderBy('id', 'desc'); $comments = $commentsQuery->paginate(10); $comments_count = $commentsQuery->count(); return view('themes/' . $this->theme_directory . '/templates/single', array_merge($this->data, [ 'categories' => $this->article_categories, 'article' => $article, 'old_article' => $old_article, 'new_article' => $new_article, 'comments' => $comments, 'comments_count' => $comments_count, 'tagline' => $article->title, ]) ); } }
In the view I have a list of comments like this:
<div id="commentsList"> <ol class="commentlist {{ boolval($is_infinitescroll) ? 'infinite-scroll' : '' }}"> @foreach ($comments as $comment) <li class="depth-1 comment"> <div class="comment__avatar"> <img class="avatar" src="{{ asset('images/avatars/' . $comment->user->avatar) }}" alt="" width="50" height="50"> </div> <div class="comment__content"> <div class="comment__info"> <div class="comment__author">{{ $comment->user->first_name }} {{ $comment->user->last_name }}</div> <div class="comment__meta"> <div class="comment__time">{{ date('jS M Y', strtotime($comment->created_at)) }}</div> <div class="comment__reply"> <a class="comment-reply-link" href="#0">Reply</a> </div> </div> </div> <div class="comment__text"> <p>{{ $comment->body }}</p> </div> </div> </li> @endforeach </ol> <div class="ajax-load text-center is-hidden"> loading... </div> </div>
Routes related to the article:
// Article routes Route::get('/', [ArticlesController::class, 'index'])->name('homepage'); Route::get('/category/{category_id}', [ArticlesController::class, 'category'])->name('category'); Route::get('/author/{user_id}', [ArticlesController::class, 'author'])->name('author'); Route::get('/show/{slug}', [ArticlesController::class, 'show'])->name('show');
I want to replace comment pagination with "infinite scroll".
For this purpose I have:
/* Infinite comments */ function infiniteComments() { var page = 1; $(window).scroll(function() { if ($(window).scrollTop() + $(window).height() >= $(document).height() - $('.s-footer').height()) { page++; loadMoreData(page); } }); } function loadMoreData(page){ var base_url = window.location.href.split('?')[0]; $.ajax({ url: `${base_url}?page=${page}`, type: "get", beforeSend: function() { $('.ajax-load').show(); } }) .done(function(data) { if (data.html == "") { $('.ajax-load').hide(); return; } $('.ajax-load').hide(); $(".infinite-scroll").append(data.html); }) .fail(function(jqXHR, ajaxOptions, thrownError) { console.log('The server is not responding...'); }); } $(document).ready(function(){ infiniteComments(); });
When visiting https://larablog.com/show/deserunt-qui-exeritationem?page=2
the comments on page 2 are displayed correctly, the Chrome console shows these 500 (internal Server Error)Error:
https://larablog.com/show/deserunt-qui-exercitationem?page=65 500 (Internal Server Error) The server is not responding... https://larablog.com/show/deserunt-qui-exercitationem?page=76 500 (Internal Server Error) The server is not responding...
The error can be traced to the error message on line 70 in ArticlesController - $article = Article::firstWhere('slug', $slug)
:
Try to get the attribute "id" of a non-object.
This is weird because $article = Article::firstWhere('slug', $slug)
works fine without Ajax.
firstWhere
Returns the first record that meets the delivery conditions. The default isnull
. So, your lineWill return the first article that
slug
matches$slug
, ornull
if no such record exists. Now, whenever you quote$article->id
, you assume that$article
is a correctArticle
and you want to know itsThe value of id
. If there are no matching articles, this will produce the error you encountered.Therefore, it is wise to check for
empty($article)
after$article
is initialized, and handle edge cases when it is indeed empty.This is my solution:
Added this new route in
routes\web.php
:In ArticlesController:
I will only load the Ajax script if there are more than 10 comments:
script: