osherdo Posted February 2, 2016 Share Posted February 2, 2016 I have this pivot table: http://3.1m.yt/0_7a5sa.png I need to return multiple names of users (from users table) that have at least one expectation_id value as the current user have. How do I achieve that? Do I need to iterate with foreach loop and use Auth::user() to do this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/300716-retrieve-data-from-pivot-table/ Share on other sites More sharing options...
osherdo Posted February 2, 2016 Author Share Posted February 2, 2016 public function mutual() { $mutualUsers = DB::table('expectation_profile')->where(expectation_id,); } I am trying to write the query now. Not done with it yet. Quote Link to comment https://forums.phpfreaks.com/topic/300716-retrieve-data-from-pivot-table/#findComment-1530688 Share on other sites More sharing options...
Psycho Posted February 3, 2016 Share Posted February 3, 2016 I had to make some assumptions on the field names in the users table: SELECT DISTINCT users.username FROM users JOIN expectation_profile ON users.profile_id = expectation_profile.profile_id WHERE users.profile_id <> $currentUserID AND expectation_profile.expectation_id IN ( SELECT expectation_id FROM expectation_profile WHERE profile_id = $currentUserID ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/300716-retrieve-data-from-pivot-table/#findComment-1530696 Share on other sites More sharing options...
osherdo Posted February 5, 2016 Author Share Posted February 5, 2016 @Psycho are you familiar with laravel by accident? Quote Link to comment https://forums.phpfreaks.com/topic/300716-retrieve-data-from-pivot-table/#findComment-1530813 Share on other sites More sharing options...
Psycho Posted February 5, 2016 Share Posted February 5, 2016 @Psycho are you familiar with laravel by accident? I have no clue about Laravel. What I posted was just an SQL query. Quote Link to comment https://forums.phpfreaks.com/topic/300716-retrieve-data-from-pivot-table/#findComment-1530814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.