Laravel eloquent: Update A Model And its Relationships

后端 未结 2 1172
终归单人心
终归单人心 2020-12-15 23:33

With an eloquent model you can update data simply by calling

$model->update( $data );

But unfortunately this does not update th

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 00:00

    You may try something like this, for example a Client model and an Address related model:

    // Get the parent/Client model
    $client = Client::with('address')->find($id);
    
    // Fill and save both parent/Client and it's related model Address
    $client->fill(array(...))->address->fill(array(...))->push();
    

    There are other ways to save relation. You may check this answer for more details.

提交回复
热议问题