Laravel - update model relationship to another model
<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>