雄辩的关系 - 附加(但不保存)到 Has Many [英] Eloquent relations - attach (but don't save) to Has Many
问题描述
我建立了以下关系:
类页面{公共功能评论(){return $this->hasMany('Comment');}}类评论{公共功能页面(){return $this->belongsTo('Page');}}
非常标准.一页可以有多条评论,一条评论属于一页.
我希望能够创建一个新页面:
$page = 新页面;
还有评论
$comment = 新评论;
并将评论附加到页面,不保存任何内容
$page->comments->associate($comment);
我尝试了以下方法:
//这些是来自 MANY 方面的一对多(例如 $comment->page->associate...)$page->comments->associate($comment);//调用未定义的方法 IlluminateDatabaseEloquentCollection::associate()$page->comments()->associate($comment);//调用未定义的方法 IlluminateDatabaseQueryBuilder::associate()//这2个是多对多关系,所以不工作$page->comments->attach($comment);//调用未定义的方法 IlluminateDatabaseEloquentCollection::attach()$page->comments()->attach($comment);//调用未定义的方法 IlluminateDatabaseQueryBuilder::attach()//这 2 个将(如果成功)保存到我不想要的数据库中$page->comments->save($comment);//调用未定义的方法 IlluminateDatabaseEloquentCollection::save()$page->comments()->save($comment);//完整性约束违规:1048 列 'page_id' 不能为空
真正奇怪的是,做相反的事情(将页面附加到评论)可以正常工作:
$comment->page()->associate($page);
相关文档在 here 但他们没有提到附加到 ONE 方面一对多的.甚至可能吗?(我觉得应该是)
听起来您只是想将新的评论对象添加到页面的评论集合中 - 您可以使用基本的集合添加方法轻松地做到这一点:
p>
$page = 新页面;$comment = 新评论;$page->comments->add($comment);
I have the following relations set up:
class Page {
public function comments() {
return $this->hasMany('Comment');
}
}
class Comment {
public function page() {
return $this->belongsTo('Page');
}
}
Pretty bog standard. One page can have many comments, and one comment belongs to a single page.
I'd like to be able to create a new page:
$page = new Page;
and a comment
$comment = new Comment;
and attach the comment to the page, without saving any of it
$page->comments->associate($comment);
I've tried the following:
// These are for one-to-many from the MANY side (eg. $comment->page->associate...)
$page->comments->associate($comment); // Call to undefined method IlluminateDatabaseEloquentCollection::associate()
$page->comments()->associate($comment); // Call to undefined method IlluminateDatabaseQueryBuilder::associate()
// These 2 are for many-to-many relations, so don't work
$page->comments->attach($comment); // Call to undefined method IlluminateDatabaseEloquentCollection::attach()
$page->comments()->attach($comment); // Call to undefined method IlluminateDatabaseQueryBuilder::attach()
// These 2 will (if successful) save to the DB, which I don't want
$page->comments->save($comment); // Call to undefined method IlluminateDatabaseEloquentCollection::save()
$page->comments()->save($comment); // Integrity constraint violation: 1048 Column 'page_id' cannot be null
The really odd thing is that doing the opposite (attaching the page to the comment) works correctly:
$comment->page()->associate($page);
The relevant docs are here but they don't make any mention of attaching to the ONE side of a one-to-many. Is it even possible? (I feel like it should be)
It sounds like you just want to add the new comment object to the page's comments collection - you can do that easily, using the basic colection add method:
$page = new Page;
$comment = new Comment;
$page->comments->add($comment);
这篇关于雄辩的关系 - 附加(但不保存)到 Has Many的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!