Search code examples
laraveleloquentmany-to-many

Delete one entry from pivot table laravel


How can i delete only one entry from many-to-many relationship pivot table in laravel if i have the id of the row?

I have 2 entities in relation of many to many. If i have users for example and user_id = 1(2 entries of this user in table) if i do users->entity()->detach() it will delete all the entries with user_id = 1, and i want to delete only the entry where pivot->id = some_id_i_give


Solution

  • Solved it. I used:

    ->wherePivot('id', '=', $pivot_id)->detach()

    and it worked great.