Laravel - update model relationship to another model
P粉674999420
P粉674999420 2023-09-01 22:36:33
0
1
542
<p>There is a many-to-many relationship between the event model and the patient model. An incident can involve multiple patients, and a patient can be involved in multiple incidents. </p> <p>If the user creates a copy of the patient model, we want to be able to merge the two patient models into one. This means I want to move the events involving patient 1 to patient 2, including other attributes on the pivot table. </p> <p>I tried simple things</p> <pre class="brush:php;toolbar:false;">Casualty::where('patient_id', $patientOne->getKey())->update(['patient_id' => $patientTwo- >getKey()]);</pre> <p>But that doesn’t work. Using the <code>updateOnExistingPivot()</code> method means I need to iterate through each event for Patient 1 and run a separate database query to update the patient to Patient 2. </p> <p>I also tried updating the record like this</p> <pre class="brush:php;toolbar:false;">$patientOne->incidents()->update(['patient_id' => $patientTwo->getKey()]);< /pre> <p>This also doesn't work because there is no <strong>patent_id</strong> column on the events table. </p> <p>How can I achieve this, or am I doing something wrong? </p>
P粉674999420
P粉674999420

reply all(1)
P粉244155277

Not sure if I understand what you mean, do you want to attribute more patients to the same incident? You can use the belongsToMany relationship and create a pivot table. Then when you want to update the data, just use the sync method.

You could also try using json_encode() to store them in a column that just holds the user ID and retrieve them later.

Sorry, can't provide more information because the problem is not well described.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template