Reputation: 65
I have spent hours on this and cannot find a way around it, I have a query that gets all friends that have the profiles ID in either the requestee or inbound section and checks if its approved, this part is correct as if i echo out the query it returns all the correct rows, now when for every row i want to loop through and search the database for the user that correlates with the requestee ID or inbound ID that was in the friends table, currently it only loops once, I know this as I loop both $rowsget
and $temp1
and the usernames from $temp1
only correlates to the first row of $rowsget
. Any help is much appreciated as my head is about to explode.
Query
$rowsget = DB::table('friends')
->where( function(Builder $query) use($uid) {
$query->where('requesteeid', $uid)
->orWhere('inboundid', $uid);
})
->where('approved', true)
->get(['requesteeid', 'inboundid']);
if(count($rowsget) > 0){
foreach($rowsget as $get) {
$temp1 = DB::table('users')
->where('id', $get->requesteeid)
->orWhere('id', $get->inboundid)
->get(['username']);
}
//Echoing data (just test )
echo $temp1;
echo $rowsget;
}
Results(The usernames are the the result from the first 2 ids )
[{"username":"Max11"},{"username":"bob11"}]
[{"requesteeid":4,"inboundid":1},{"requesteeid":4,"inboundid":2}]
Upvotes: 1
Views: 1021
Reputation: 65
Just fixed this
foreach($rowsget as $get) {
$temp1 = DB::table('users')->where('id', $get->requesteeid)->orWhere('id',
$get->inboundid)->get(['id', 'username', 'avatar']);
foreach ($temp1 as $key) {
if($key->id != $uid) {
echo $key->username;
}
}
Upvotes: 0
Reputation: 1301
You are overwriting the entry in $temp1
and you are printing it outside the loop hence it will always give you the last entry.
Make it an array to have all the entries in it:
$temp1 = [];
if(count($rowsget) > 0){
foreach($rowsget as $get) {
$temp1[] = DB::table('users')->where('id', $get->requesteeid)-
>orWhere('id', $get->inboundid)->get(['username']);
}
print_r($temp1);
Upvotes: 3