在 one-to-one 关系中,create() 可以替代 associate() 吗?

can create() act as an alternate to associate() in a one-to-one relationship?

假设有两个类,

1- User
2- Contact

他们是一对一的关系:

User ->hasOne(Contact)
Contact ->belongsTo(User)

现在根据 Laravel 5.x 文档,我需要 "associate" child 到 parent。

$contact = Contact::create($input);
$user = Auth::User();
$contact->user()->associate($user);

但我看到代码执行相同的操作 "association",如下所示:-

$user->contact()->create($input);

假设我的理解是正确的,这两者都会导致相同的结果。那么为什么 Laravel 文档中没有提到这一点,对于初学者来说,遵循 "all over the place" .

的文档真的很混乱

其次,如果这是一个 hack 或绕过的方法,那么将两者进行比较意味着什么。

是一样的,显然有时取决于你的应用程序,需要关联方式,但如果你可以选择 $user->contact()->create($input); 作为它的 shorter/cleaner。