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

ssquare's avatar

Laravel query builder returning more rows than intended

Have a table structure like:

 properties
 ------------
 id
 property_name

 buildings
 -------------
 id
 building_name
 property_id

 floors
 -------------
 id
 floor_coode
 building_id

 units
 -------------
 id
 unit_number
 floor_id

 amenities
 ------------
 id
 amenity_name
 amenity_value
 unit_id

 categories
 -----------
 id
 category_name
 parent_id

amenity_categories
 ------------
amenity_id
category_id


Now, I tried to run following query:

        $query = \DB::table('amenities')
            ->join('units', 'units.id', 'amenities.unit_id')
            ->join('floors', 'floors.id', 'units.floor_id')
            ->join('buildings', 'buildings.id', 'floors.building_id')
            ->join('properties', 'properties.id', 'buildings.property_id')
            ->leftjoin('amenity_category', 'amenities.id', 'amenity_category.amenity_id')
            ->leftjoin('categories', 'categories.id', 'amenity_category.category_id')
            ->select('amenities.id as ame_id', 'amenities.amenity_name', 'amenities.amenity_value','units.id as unit_id', 'units.unit_number','amenity_category.*','categories.parent_id as cat_parent_id')
            ->where('buildings.id',$building_id)
            ->orderBy('units.unit_number','asc');

        $data = $query->get();

With this, it is returning more row than it should be for example lets look a chunk of particular unit:107

[300] => stdClass Object
                (
                    [ame_id] => 2
                    [amenity_name] => CU
                    [amenity_value] => 20
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 2
                    [category_id] => 8
                    [cat_parent_id] => 5
                )

            [301] => stdClass Object
                (
                    [ame_id] => 3
                    [amenity_name] => VR
                    [amenity_value] => 50
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 
                    [category_id] => 
                    [cat_parent_id] => 
                )

            [302] => stdClass Object
                (
                    [ame_id] => 4
                    [amenity_name] => SF
                    [amenity_value] => 320
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 4
                    [category_id] => 6
                    [cat_parent_id] => 5
                )

            [303] => stdClass Object
                (
                    [ame_id] => 1
                    [amenity_name] => BO
                    [amenity_value] => 20
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 1
                    [category_id] => 6
                    [cat_parent_id] => 5
                )

            [304] => stdClass Object
                (
                    [ame_id] => 4
                    [amenity_name] => SF
                    [amenity_value] => 320
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 4
                    [category_id] => 9
                    [cat_parent_id] => 5
                )

            [305] => stdClass Object
                (
                    [ame_id] => 1
                    [amenity_name] => BO
                    [amenity_value] => 20
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 1
                    [category_id] => 8
                    [cat_parent_id] => 5
                )

            [306] => stdClass Object
                (
                    [ame_id] => 4
                    [amenity_name] => SF
                    [amenity_value] => 320
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 4
                    [category_id] => 10
                    [cat_parent_id] => 5
                )

            [307] => stdClass Object
                (
                    [ame_id] => 1
                    [amenity_name] => BO
                    [amenity_value] => 20
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 1
                    [category_id] => 9
                    [cat_parent_id] => 5
                )

            [308] => stdClass Object
                (
                    [ame_id] => 4
                    [amenity_name] => SF
                    [amenity_value] => 320
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 4
                    [category_id] => 11
                    [cat_parent_id] => 5
                )

            [309] => stdClass Object
                (
                    [ame_id] => 1
                    [amenity_name] => BO
                    [amenity_value] => 20
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 1
                    [category_id] => 10
                    [cat_parent_id] => 5
                )

            [310] => stdClass Object
                (
                    [ame_id] => 2
                    [amenity_name] => CU
                    [amenity_value] => 20
                    [unit_id] => 1
                    [unit_number] => 107
                    [amenity_id] => 2
                    [category_id] => 6
                    [cat_parent_id] => 5
                )

Here, you can see actually there are only 4 different ame_id ranging from 1-4 and that how it should be returned. Where I am doing wrong? Also, removing that pivot table is not the idea as I also need to load that relation data as well.

There are no duplicate rows, for more observation you can focus on the index 305 and 307 they are just different because of different category_id. I think I need to do something like with in eloquent. But, I don't want to use eloquent as I feel that it is quite slow as compared to the query builder.

0 likes
3 replies
Snapey's avatar

What is parent_id in categories?

ssquare's avatar

@snapey It is the foreign key of the id of the same table. If it is parent id itself then it would have 0 as parent_id else if it is a child then it would possess the id of parent category in parent_id

Could I get all the categories details based on that ame_id with some subquery or something like that? I think that should be the problem so far.

Snapey's avatar

The problem is that you are getting all permutations of amenity and category. If this is not what you want, remove category from the joins.

Please or to participate in this conversation.