SpiderWasp42
SpiderWasp42

Reputation: 2656

Delete a pivot table row without knowing one of the foreign keys

I currently have a pivot table (pending invites) joining a groups table and contacts table. There are 4 columns:

id | group_id | contact_id | email

email field is simply the email id of the contact involved. I have a situation where I need to remove a pending invite when I know the group_id and the corresponding email, but not the contact id. The standard detach function:

$groups->contacts()->detach($contact_id);

requires the contact id to be supplied. Is there a workaround to this?

Upvotes: 0

Views: 91

Answers (1)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

You can just manually remove selected rows from the pivot table:

\DB::table('contacts_groups')
  ->whereGroupId($groupId)
  ->whereEmail($email)
  ->delete();

Upvotes: 1

Related Questions