Reputation: 46
Concept: I tried to retrieve the common pages from 9999 and 1111. See below. (ACF Fiorentia)
mysql query: SELECT * from fb_likes
where page_id
IN (
SELECT page_id
FROM fb_likes
WHERE user_fb_id
= 9999)
and user_fb_id
= 1111
PHP Code:
$temp = FBLikes::where('user_fb_id','=','1111')->get(array('page_id'));
$tempArr = array();
for($i = 0; $i < count($temp); $i++){
$tempArr = array_add($tempArr, $i, $temp[$i]['page_id']);
}
print_r($tempArr);
$common_likes = FBlikes::whereIn('page_id', $tempArr)->where('user_fb_id','=', '9999')->get();
I tried using eloquent. It was not successful.
Steps followed: I executed the sub query first. Then dumped the data to $tempArr. Then I passed the array $tempArr to main query.
Result: Nothing is saved in $common_likes.
Could you please help me to write the mysql sub queries in single Eloquent statement? Thank you.
Upvotes: 2
Views: 70
Reputation: 7023
you build invalid array, and you don't have to do that. try to replace $tempArr
with $temp
and remove all this code:
$tempArr = array();
for($i = 0; $i < count($temp); $i++){
$tempArr = array_add($tempArr, $i, $temp[$i]['page_id']);
}
print_r($tempArr);
you don't need it.
Upvotes: 1