3

I have a question about Laravel's relation model

comment table        id post_id

post table           id author_id

author table         id name

I want use relation model to get author's name in comment model through post table, how to realize this. Thanks a lot.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
陈孝右
  • 33
  • 1
  • 5

4 Answers4

5

Relationships in Laravel are pretty straight forward to implement and are really easy to comprehend because of the expressive API. So in your case the logic would be this:

A Comment belongs to a Post and a Post belongs to a Author

So considering you have a Comment, Post and Author models, each one should have a relationship method defined that reflects the logic above.


The Comment model would look like this:

class Comment extends Model
{
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

And the Post model would look like this:

class Post extends Model
{
    public function author()
    {
        return $this->belongsTo(Author::class);
    }
}

Then you can easily access the post author name via those relationships:

Comment::find(1)->post->author->name;

As you can see from the code above writing code for the relationships is very easy since the method names are really suggestive. You can read more about Eloquent Relationships in the Laravel Documentation.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
1
class Post extends Model
{
    public function author()
    {
        return $this->belongsTo(Author\class)->select('name');
    }
}

Comment::with('author')->find(1);

in blade 

{{$comment->auther['name']}}
Mohanad Hilles
  • 87
  • 1
  • 3
  • 12
1

Here's my answer using Laravel 8 and belongsTo.

In my solution, I created first the relationship User and Post

 // Post migration

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id(); 
            $table->unsignedBigInteger('user_id'); 
      
            $table->string('title',160)->unique();
            $table->string('image');
            $table->text('content')->nullable();
            $table->timestamps();
            
            $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

        });
    }

In My Post model I created the authors function

// Post Model
public function authors()
     {
         return $this->belongsTo(User::class, 'user_id','id');
     }

Where the 'user_id' is the foreignKey, and id is ownerKey

Now I Call Post model in my controller using this

//Post Controller

  //return latest posts with the author and paginate it
    public function index()
    {
        $posts = Post::latest()->with('authors')->paginate(3);

        return view('blog',compact('posts'));

    }

And finally, I use it in blog.blade file

//blog.blade
<hr>
@foreach ($posts as $post)

    <h3>{{$post->title}}</h3><pre>{{$post->role}}</pre> 
    //get author name forech post
    <pre>{{$post->authors['name']}}</pre>
@endforeach
<hr>
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

You have to create a hasMany relationship between the post and the user. To do this you have to write this relationship function in the models:

Post model:

// A post can have 1 author
public function author() {
    return $this->belongsTo('App\User');
}

User model:

// A user can have multiple posts
public function posts() {
    return $this->hasMany('App\Post');
}
Michiel
  • 123
  • 1
  • 9