Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Schema::create('comments', function (Blueprint $table) {
- $table->increments('id');
- $table->unsignedInteger('user_id');
- $table->unsignedInteger('post_id');
- $table->unsignedInteger('reply_id')->default(0);
- $table->text('body');
- $table->timestamps();
- });
- class Comment extends Model
- {
- protected $fillable = [
- 'user_id',
- 'post_id',
- 'reply_id',
- 'body'
- ];
- public function user()
- {
- return $this->belongsTo(User::class);
- }
- public function post()
- {
- return $this->belongsTo(Post::class);
- }
- public function replies()
- {
- return $this->hasMany(Comment::class,'reply_id','id');
- }
- $comments = Comment::with('replies')->where('reply_id','=',0)->get(['id','reply_id','body']);
- return response($comments);
- public function replies() {
- return $this->hasMany('AppComment', 'parent_id');
- }
- @foreach($comments as $comment)
- {{ $comment->content }}
- @if ( $comment->replies )
- @foreach($comment->replies as $rep1)
- {{ $rep1->content }}
- ...
- @endforeach
- @endif
- @endforeach
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement