2

Post Model

class Post extends Model
{


    public function comments()
    {
        return $this->hasMany('App\Comment');
    }

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

Comment Model

class Comment extends Model
    {

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


   }  

User Model

class User extends Model
{


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

Now my question is how i can access all comments that belong to post with commented username Thank you in advance

6
  • having only username you'd be able only to get all comments to ALL posts owned by given user, for this you can use hasManyThrough relation
    – zakius
    Sep 4, 2015 at 6:58
  • @zakius.thank you for the comment.can you post your answer Sep 4, 2015 at 7:00
  • Try this user::with('post.comment') -> get()
    – mdamia
    Sep 4, 2015 at 7:02
  • @mdamia.thank you.is it possible to access in single query.i need to display Post content with author name and all comments belongs to that post with username Sep 4, 2015 at 7:04
  • I will post something as answer, give it a shot.
    – mdamia
    Sep 4, 2015 at 7:06

3 Answers 3

1

Try this, it will return all posts with comments, and posts with authors for the selected user.

$data = User::with('post.comment') 
        -> with('post.author') 
        -> Where('id',$user_id) 
        -> first();

This will fetch posts with authors and post with comments with users who made the comments. Assuming your models are set up this way,

Post belongs to an Author,   
Author has many Post,  
Post has many Comment  
Comment belongs to a Post  
Comment belongs to a User  
User has many Comment.   

$posts = Post::with('author') -> with('comment.user') -> get(); 
3
  • my requirement is .i need to display Post with post auther name and along with comments that belong to that post with commented username.if you see my table structure.i am storing commented user id means forign key Sep 4, 2015 at 7:16
  • @madamia.i get collection object correctly.but at the end i get error like Whoops, looks like something went wrong. 1/1 BadMethodCallException in Builder.php line 2025: Call to undefined method Illuminate\Database\Query\Builder::user() \ Sep 7, 2015 at 4:46
  • @vision check that user method is defined
    – mdamia
    Sep 7, 2015 at 5:10
1

I think your problem is simple to solve with hasmanythrough relationship (check http://laravel.com/docs/5.1/eloquent-relationships).

0
1

You can do a query for comment relationships, like the following:

$post_id = 7;
$username = 'username';

$comments = Comment::where('post_id', $post_id)->whereHas('user', function($q) use($username) {
    $q->where('username', $username);
})->get();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.