Inserting & Updating Related Models (Belongs To Relationships)
https://viblo.asia/p/eloquent-relationships-in-laravel-phan-3-MJykjmxyePB
Belongs To Relationships
Khi cập nhật 1 belongsTo
relationship, bạn có thể sử dụng associate
method. Method này sẽ set foreign key trên model con.
$account = App\Account::find(10);
$user->account()->associate($account);
$user->save();
Khi loại bỏ belongsTo
relationship, bạn có thể sử dụng dissociate
method. Method này sẽ thiết lập các foreign key thành null
.
$user->account()->dissociate();
$user->save();
PreviousInserting & Updating Related Models (The Create Method)NextInserting & Updating Related Models (Many To Many Relationships)
Last updated
Was this helpful?