假设现在两个 Model
Post 文章
class Post extends Model {
// 省略
public function comments() {
return $this->hasMany(Comment::class);
}
}
Comment 评论
class Comment extends Model {
// 省略
public function post() {
return $this->belongsTo(Post::class);
}
}
// 预加载,可以在取得文章同时把关联的Comment一同取出
Post::with('comments')->find(1)->get();