假设我们有一对多的关系,比如post和comment,每个帖子都有hasMany评论,每个评论belongsTo一个帖子。问题是我们如何调用associate方法?在Laravel文档中,associate方法在关系方法上被调用,类似于:
$comment = new Comment(['title' => 'something']);
$post->comments()->associate($comment);但在某些情况下,我看到在关系属性上调用associate方法,如下所示:
$comment = new Comment(['title' => 'something']);
$post->comments->associate($comment);它们是一样的吗?它们之间有什么不同吗?
谢谢
https://stackoverflow.com/questions/62176736
复制相似问题