mercredi 19 juillet 2017

laravel one to many to many relationship

Hi I am new to Laravel and currently in am using Laravel 5.2. I am trying to create an application where I have posts and comments and comments have replies table and have the following model.

Post model

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

Comment model

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

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

Reply Model

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

What I want to achieve :

Post with comment with replies for Post

post -> many comments -> many replies

I am able to get comments with following

$posts = Post::with('comments')->get();

But dont know how to get comments with reply



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tpnWV3
via IFTTT

Aucun commentaire:

Enregistrer un commentaire