Support the ongoing development of Laravel.io →
Eloquent Blade Database
Last updated 2 years ago.
0

if comments belongs to a post you can call it that way inside a foreach loop

0

Comment Model:

    public function post(){
        return $this->belongsTo('App\Post');
    }




In your Controller
$recentcomment = Comment::latest(); 
// I believe latest gives you an array of latest comments. 
// If you only want one comment use latest()->first();

foreach($recentcomment as $comment){
  $post_title = $comment->post->title;
}
Last updated 5 years ago.
0

Thanks works like charm.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ghost ghostly Joined 19 Apr 2019

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.

© 2024 Laravel.io - All rights reserved.