Laravel eloquent relationship

Posted 2 months ago by softwebglobe

Hello please help me, i have three tables and models

users table blogs table replies table

and the models are as below User model Blog model Reply model

the relationship i have so far are

User.php

public function blogpost()
{
    return $this->belongsTo(Blog::class);
}

public function replies()
{
    return $this->belongsTo(Reply::class)
}

Also my Blog model is as defined below

Blog.php

public function user()
{
    return $this->belongsTo(User::class);
}

public function replies()
{
    return $this->hasMany(Reply::class)
}

and my Reply model is defined as below

Reply.php

public function blogpost()
{
    return $this->belongsTo(Blog::class);
}

public function user()
{
    return $this->belongsTo(User::class)
}

my reply migration has the following table columns

$table->integer('user_id')->unsigned();
$table->integer('blog_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('blog_id')->references('id')->on('blogs')->onDelete('cascade');

Now my question is how do i return all the blogpost which the passed user has replied to. Example user with username Trump77 has replied to several blog posts how do i return this blogs. My user model uses username route binding and id, my blog model uses slug as primary key.

I have been trying it but it keep returning me the blog id, reply id and user.

I just want to retrieve only the full blog posts the user has replied to

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.