Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. Schema::create('comments', function (Blueprint $table) {
  2. $table->increments('id');
  3. $table->unsignedInteger('user_id');
  4. $table->unsignedInteger('post_id');
  5. $table->unsignedInteger('reply_id')->default(0);
  6. $table->text('body');
  7. $table->timestamps();
  8. });
  9.  
  10. class Comment extends Model
  11. {
  12. protected $fillable = [
  13. 'user_id',
  14. 'post_id',
  15. 'reply_id',
  16. 'body'
  17. ];
  18.  
  19. public function user()
  20. {
  21. return $this->belongsTo(User::class);
  22. }
  23.  
  24. public function post()
  25. {
  26. return $this->belongsTo(Post::class);
  27. }
  28.  
  29. public function replies()
  30. {
  31. return $this->hasMany(Comment::class,'reply_id','id');
  32. }
  33.  
  34. $comments = Comment::with('replies')->where('reply_id','=',0)->get(['id','reply_id','body']);
  35. return response($comments);
  36.  
  37. public function replies() {
  38. return $this->hasMany('AppComment', 'parent_id');
  39. }
  40.  
  41. @foreach($comments as $comment)
  42. {{ $comment->content }}
  43.  
  44. @if ( $comment->replies )
  45. @foreach($comment->replies as $rep1)
  46. {{ $rep1->content }}
  47. ...
  48. @endforeach
  49. @endif
  50. @endforeach
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement