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

FounderStartup's avatar

Correct query to count with relation

I am working on a real estate site. Need the count of listings in a project ( building ) of a particular city.

 $listingcount = App\Models\Listings::where('project.city',$item->id)->count();
                            							

Listings model has a relation to project() and projects has a relation to city().

What is my correct query to count ?

0 likes
3 replies
FounderStartup's avatar

Hey , thanks for your time. But its giving syntax error.

I tried this

$listingcount = App\Models\Listings::has('project.city',$item->id)->count();

The total count of listings is correct but the city is not correct.

FounderStartup's avatar

error

syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')'

Please or to participate in this conversation.