У меня установлены следующие отношения:
class Page {
public function comments() {
return $this->hasMany('Comment');
}
}
class Comment {
public function page() {
return $this->belongsTo('Page');
}
}
Довольно болотный стандарт. На одной странице может быть много комментариев, и один комментарий относится к одной странице.
Я хотел бы иметь возможность создать новую страницу:
$page = new Page;
и комментарий
$comment = new Comment;
и прикрепите комментарий к странице, не сохраняя его.
$page->comments->associate($comment);
Я пробовал следующее:
// These are for one-to-many from the MANY side (eg. $comment->page->associate...)
$page->comments->associate($comment); // Call to undefined method Illuminate\Database\Eloquent\Collection::associate()
$page->comments()->associate($comment); // Call to undefined method Illuminate\Database\Query\Builder::associate()
// These 2 are for many-to-many relations, so don't work
$page->comments->attach($comment); // Call to undefined method Illuminate\Database\Eloquent\Collection::attach()
$page->comments()->attach($comment); // Call to undefined method Illuminate\Database\Query\Builder::attach()
// These 2 will (if successful) save to the DB, which I don't want
$page->comments->save($comment); // Call to undefined method Illuminate\Database\Eloquent\Collection::save()
$page->comments()->save($comment); // Integrity constraint violation: 1048 Column 'page_id' cannot be null
Самое странное, что обратное действие (прикрепление страницы к комментарию) работает корректно:
$comment->page()->associate($page);
Соответствующие документы находятся здесь, но в них не упоминается присоединение к ОДНОЙ стороне модели. -слишком много. Это вообще возможно? (Я чувствую, что так и должно быть)