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

<?php

   $user->roles()->detach();
?>

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.

Answers


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

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

Post Status

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

Search




Leave an answer