Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

galiavni's avatar

updating a record on a pivot table

Hi

I have a pivot table which is patient_medication in this pivot I have a few more fields like - start_date end_date and remarks.

I need to update the pivot but I am unable to grab the id of the pivot itself . Patients can have the same medications over and over again so I need to update a specific record and not the connection

Thank you

Gali

0 likes
12 replies
bestmomo's avatar

Extracted from documentation :

Updating A Record On A Pivot Table

Sometimes you may need to update your pivot table, but not detach it. If you wish to update your pivot table in place you may use updateExistingPivot method like so:

User::find(1)->roles()->updateExistingPivot($roleId, $attributes);
20 likes
kfirba's avatar

@bestmomo Your answer is almost right.

@galiavni You have to define in the relation ship your pivot table attributes:

public function roles()
{
    return $this->belongsToMany('App\Role')->withPivot('additional', 'attributes', 'here');
}

Now you can do as @bestmomo suggested:

User::find(1)->roles()->updateExistingPivot($roleId, $attributes);
8 likes
galiavni's avatar

Thank you for your reply , I have gone through this documentation over and over again . I shall rephrase my question. In order to update I need a form , How do I link to the form ? how do I know that I am on the right record ? The role-id is not enough because I have a couple of records with the same user and the same role_id . How do I link to the exact pivot ?

bestmomo's avatar

Do you mean you have in your pivot table records with same couple of foreign keys ?

Kryptonit3's avatar

Wouldn't using sync accomplish the same thing?

$user->find(1)->roles()->sync(array(1, 2, 3));
galiavni's avatar

Exactly , I have same couple of foreign keys. Is there a problem about it ?

bestmomo's avatar

No idea if it's a problem for Eloquent because I never tried to manage this, it's not usual.

For example maybe updateExistingPivot cant work.

You need a field on pivot table to distinguish records for your query, maybe a date.

galiavni's avatar

I have a date field and the table has an "id" for each record. My question is how do I get the "id" in order to know which record I am supposed to update.

JarekTkaczyk's avatar

@galiavni You can do this:

public function medications()
{
  return $this->belongsToMany('Medication', 'patient_medication')->withPivot('id', ... other fields you need)->withTimestamps();
}

// then
$patient->medications->first()->pivot->id; // this is what you need, so pass it in the form

// upon update
$patient->medications()
    ->newPivotStatement()
    ->where('id', $id)->update(....)

Mind that newPivotStatement will be instance of Query\Builder, ie. no Eloquent features, no timestamp auto update.

Alternatively, depending on your app, you could rely on the timestamp of the association:

$patient->medications->first()->pivot->created_at; // that's what we need, again passed to the update controller

$patient->medications()
    ->newPivotStatementForId($medicationId)
    ->where('created_at', $createdAt)
    ->update( .... ); // again, raw `Query\Builder` object, so you need to take care of `updated_at` column

// for consistency you can use freshTimestamp for the updated_at field
$updatedAt = $patient->freshTimestamp();
12 likes
galiavni's avatar

Thank you JarekTkaczyk , your solution did solve my problem. The only thing which was included was to include the 'id' of the pivot in the '->withpivot' phrase.

Roni's avatar

This was a phenomenally helpful thread! I know it's old but @galiavni, you may want to award the correct answer so people searching see it as solved. Depending on urgency, I often skip over unsolved threads on the subject I'm looking for.

3 likes

Please or to participate in this conversation.