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 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>
As it is a form, it is using \n instead of html content (treating it as normal text)
Instead of that use {!! nl2br($msg)!!}
it will work.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community