Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FounderStartup's avatar

Nested query with OR ?

Controller :

$members = InnerCircleMember::where('circle_id', $id)->where('status', 1)->paginate(10);
$listingsforsale = Listings::where('user_id',$user->id)->where('expiry_date','>' , now())->where('listingtype', 1)->where('status',1)->paginate(10);

I need $listingsforsale where 'user_id' is equal to ( $members->member_id OR $members->admin_id )

wha will be my correct query ? I tried few options but not getting correct result :) Any help ?

0 likes
15 replies
FounderStartup's avatar

@Sinnbeck

Is it correct ?

        $listingsforsale = Listings::
            where(function ($query) use ($members){
                $query->where('user_id', $members->admin_id)
                      ->orWhere('user_id', $members->member_id);
            })
            ->where('expiry_date','>' , now())
            ->where('listingtype', 1)
            ->where('status',1)
            ->paginate(10);
Sinnbeck's avatar

@FounderStartup Yes. But it can be shorted to this, as its the same column. But be aware that the query will fail, as $members is a pagination instance, and not a single member. I am showing you syntax here, not how to solve the logic problem with the code (unless that is the question)

listingsforsale = Listings::whereIn('user_id', [$members->admin_id, $members->member_id])
            ->where('expiry_date','>' , now())
            ->where('listingtype', 1)
            ->where('status',1)
            ->paginate(10);
1 like
Sinnbeck's avatar

@FounderStartup Then you will need to explain what you are trying to do. You are getting 10 "random" rows.. And you want to just get listingsforsale for those specifically, but as a separate paginated list? So if you change page on members, you might suddenly get completely different admin_ids and member_ids ?

1 like
FounderStartup's avatar

@Sinnbeck

Let me try to explain again : I need to show listings for sale by users who are members of a particular inner circle.

now controller is :

    $member = InnerCircleMember::where('circle_id', $id)->where('status', 1)->get();

$listingsforsale = Listings::
            where(function ($query) use ($member){
                $query->where('user_id', $member->admin_id)
                      ->orWhere('user_id', $member->member_id);
            })
            ->where('expiry_date','>' , now())
            ->where('listingtype', 1)
            ->where('status',1)
            ->paginate(10);
Sinnbeck's avatar

@FounderStartup Ok so you need whereIn

$listingsforsale = Listings::
            where(function ($query) use ($member){
                $query->whereIn('user_id', $member->pluck('admin_id'))
                      ->orWhereIn('user_id', $member->pluck('member_id'));
            })
            ->where('expiry_date','>' , now())
            ->where('listingtype', 1)
            ->where('status',1)
            ->paginate(10);
1 like
FounderStartup's avatar

@Sinnbeck Thanks chief once again. Where exactly I am lacking to handle such queries ? :) Can you share any link of documentation for such nested queries ?

Sinnbeck's avatar

It was the link I shared in my first post? Only difference is that I changed it to use whereIn instead of where, and you are checking a list of items (multiple members) instead of one

1 like

Please or to participate in this conversation.