Support the ongoing development of Laravel.io →
Authorization Blade

I am working on a blogging application in Laravel 8.

The ArticlesController controller I have this method to display a single article, with comments:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\ArticleCategory;
use App\Models\Article;
use App\Models\Comment;

class ArticlesController extends FrontendController {

	// More code

	public function show($slug) {
		// Single article
		$article = Article::firstWhere('slug', $slug);
		$comments = Comment::where('article_id', $article->id)->orderBy('id', 'desc');
		return view('themes/' . $this->theme_directory . '/templates/single', 
			array_merge($this->data, [
				'categories' => $this->article_categories,
				'article' => $article,
				'comments' => $comments,
				'tagline' => $article->title,
				])
			);
	}
	
}

The problem

The problem is that the folowing piece of code intended to display the article comments throws an Parameter must be an array or an object that implements Countable error:

@if ($comments)
<h3>{{ count($comments) }} comments</h3>
<!-- START commentlist -->
<ol class="commentlist">
	@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>

Questions

  1. What causes this bug?
  2. What is the easiest fix?
Last updated by @ajax30 2 years ago.
0
  1. What causes this bug?

As it is a form, it is using \n instead of html content (treating it as normal text)

  1. What is the easiest fix?

Instead of that use {!! nl2br($msg)!!} it will work.

Last updated 2 years ago.

ajax30 liked this reply

1
Solution selected by @ajax30

@ajax30 pleas don't link to other threads and keep this one on topic.

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Razvan ajax30 Joined 2 Oct 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.