简体   繁体   中英

How can I update data which is saved with relatiosnships in Laravel?

I want to update my data with the help of relationship and I already stored this data with the help of relationship. I already stored data in another table. Creation of data is given below:

$data = array('value' => $request->value,);

$followup = FollowupType::create($data);
$parent_id=$request->organization;
$personable = Personne::find($parent_id);
$personable->followup_types()->save($followup);

Now I want to know how can I update data?

If your personable and followup_type relationship is many-to-many , you can use sync to update, create or delete relationship automatically.

$personable->followup_types()->sync($data);

However, if the relationship is one-to-many , unfortunately there is no sync method for one-to-many relations. You can do it like this:

$personable->follow_types()->delete();
$personable->followup_types()->saveMany($data);

If the relationship is one-to-one , just update it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM