Right now in the User Dashboard view (home.blade.php) it wrongly fetches the comments count of the first post by the user.
I would like to display the correct comments count of a post next to the posts’ title.
What am I missing/doing wrong?
View (home.blade.php):
@if(count($posts) > 0) <h5>Your Posts ({{ count($posts) }})</h5><hr> @foreach($posts as $post) <h6><a href="/posts/{{ $post->id }}" class="btn btn-link"> {{ $post->title }} </a> {{ $comments_count }} comments </h6><hr> @endforeach @endif
Controller:
class HomeController extends Controller { public function __construct() { $this->middleware('auth'); } public function index() { $user_id = auth()->user()->id; $user = User::find($user_id); $post = Post::with('commentsCount')->first(); $comments_count = $post->commentsCount->first()->aggregate; return view('home')->with('posts', $user->posts) ->with('comments_count', $comments_count); } }
Models (User, Post, Comment):
class User extends Authenticatable { use Notifiable; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; protected $casts = [ 'email_verified_at' => 'datetime', ]; public function posts() { return $this->hasMany('AppPost'); } } class Post extends Model { protected $table = 'posts'; public $primaryKey = 'id'; public function user() { return $this->belongsTo('AppUser'); } public function comments() { return $this->hasMany('AppComment'); } public function commentsCount() { return $this->comments() ->selectRaw('post_id, count(*) as aggregate') ->groupBy('post_id'); } } class Comment extends Model { protected $table = 'comments'; public $primaryKey = 'id'; public function post() { return $this->belongsTo('AppPost'); } }
Migrations (CreatePostsTable, CreateCommentsTable):
public function up() { Schema::create('posts', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('user_id'); $table->string('title'); $table->mediumText('body'); $table->timestamps(); }); } public function up() { Schema::create('comments', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('author'); $table->integer('post_id'); $table->integer('user_id'); $table->mediumText('comment'); $table->timestamps(); }); }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Your Post model hasMany
comments, so you can able to use count()
method, like : $post->comments->count()
:
@if(count($posts) > 0) <h5>Your Posts ({{ count($posts) }})</h5><hr> @foreach($posts as $post) <h6><a href="/posts/{{ $post->id }}" class="btn btn-link"> {{ $post->title }} </a> {{ $post->comments->count() }} comments </h6> <hr> @endforeach @endif
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0