John Davidson

php - Laravel check if belongstomany contains belongstomany

0 comments
Message:


in my system:



  • a lead belongstomany salespeople

  • a manager belongstomany salespeople


i am trying to check if a lead has a manager through the salespeople. this is for a policy so i can make sure a manager can see the leads of their salespeople.


something like this:


$lead->salespeople->contains($manager->salespeople)

is there a collection method that will allow me to do this? i've also tried stuff like this which isn't working either:


$lead->salespeople->contains('id', $manager->salespeople->pluck('id')->toArray())

edit, i think i got it. does this look correct?:


$lead->salespeople->intersect($manager->salespeople)->count() > 0


My Answer:
salespeople()->pluck('id')->contains($manager->id);

Rate this post

5 of 5 based on 6881 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us