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

FounderStartup's avatar

nested query issue

I need '$similarlistings' with city = $listing->city. Its a simple query but I may be missing some basics here :)

controller



    public function ListingDetails($id)
    {

        $listing = Listing::findOrFail($id);
        Listing::find($id)->increment('views');
        $responses = ListingResponse::where('listing_id', $id)->paginate(10);
        $totalresponses = ListingResponse::where('listing_id', $id)->count();
        
        $similarlistings = Listing::with('cityname','listingpropertytypename','listingtypename')->take(2)->get();

        return view('frontend.listing-details', compact('similarlistings','listing', 'responses', 'totalresponses'));
    }

0 likes
15 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You give it an array with a function. You can you both examples together if want to filter both

Listing::with(['cityname' => function ($q) use ($listing) {
    $q->where('name', $listing->city);
}])

//or filter listings
Listing::whereHas('cityname', function ($q) use ($listing) {
    $q->where('name', $listing->city);
})
1 like
FounderStartup's avatar

@Sinnbeck Thanks . And what if I need to add another query ? like I need "$similarlistings" with city = $listing->city and propertytype = $listing->propertytype ? Need to chain it or can be used in same bracket ?

Sinnbeck's avatar

@FounderStartup either. Personally I would make one for each, but both works

Listing::with(['cityname' => function ($q) use ($listing) {
    $q->where('name', $listing->city);
}])->with('listingpropertytypename','listingtypename') 
1 like
FounderStartup's avatar

@Sinnbeck

   $similarlistings = Listing::with(['cityname' => function ($q) use ($listing) {
            $q->where('id', $listing->city);
        }])

        ->with(['listingpropertytypename' => function ($q) use ($listing) {
            $q->where('id', $listing->property_type);
        }])

        ->with('cityname','listingpropertytypename','listingtypename')->take(2)->get();

But I am not getting the required result ? what I am missing here ?

Sinnbeck's avatar

@FounderStartup I assume you want me to guess what you want and what you are getting? I then guess that you need to add a whereHas also. That will filter the listings. It's in my first post

What you have now, filters the relationship, and whereHas filters the main query

1 like
FounderStartup's avatar

@Sinnbeck :)

I just want a query to get "$similarlistings" with city = $listing->city and propertytype = $listing->propertytype ?

The query worked for CITY but failed for property_type.

Sinnbeck's avatar

@FounderStartup all good. Just remember to tell us what does not work, as it is often impossible to guess :)

1 like
FounderStartup's avatar

@Sinnbeck Thanks for understanding :)

I need a query to get "$similarlistings" with city = $listing->city and propertytype = $listing->propertytype ?

Presently the following query is not filtering the data.

controller :



        $listing = Listing::findOrFail($id);
        Listing::find($id)->increment('views');
        $responses = ListingResponse::where('listing_id', $id)->paginate(10);
        $totalresponses = ListingResponse::where('listing_id', $id)->count();

        $similarlistings = Listing::with(['cityname' => function ($q) use ($listing) {
            $q->where('id', $listing->city);
        }])
        ->with(['listingpropertytypename' => function ($q) use ($listing) {
            $q->where('id', $listing->property_type);
        }])
        ->with('cityname','listingpropertytypename','listingtypename')->take(4)->get();

Sinnbeck's avatar

@FounderStartup they do different things.

With = loads a relationship as children. If you use an array you can limit the children (not the parent). Imagine saying to a group of mothers that they can only bring girls. That still means all mothers come, even if they have no girls

WhereHas = limit the parents. Same example. We this time only want mothers who has girls.

1 like

Please or to participate in this conversation.