Home Ask Login Register

Developers Planet

Your answer is one click away!

SarangaR February 2016

Laravel 5.1 detach with where

I want detach without some records

my role_user table is id | role_id | user_id



I don't want detach all records some records need (I mean find solution to where clause inside detach() ). How can I do this? Thank you.


oseintow February 2016

You can use sync and pass in the array of ids you dont want delete. The inverse is to use detach and pass in an array of ids you want to delete.

SarangaR February 2016

Finnaly I found the solution

$user->roles()->wherePivot('role_id', '!=', 3)->detach();

Post Status

Asked in February 2016
Viewed 2,014 times
Voted 5
Answered 2 times


Leave an answer

Quote of the day: live life