更新时间:2023-09-29 14:23:22
假設現在兩個 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();
是要這樣嗎?