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

rubu710's avatar

Eloquent - Some relationships missing when using query ->with()

I have the following:

Garment.php:

public function properties(){
        return $this->belongsToMany('App\Property');
    }

Property.php:

public function garments(){
        return $this->belongsToMany('App\Garment');
    }

Following laravel convention for table names : Garments, Properties. Fks: garment_id, property_id . They have a many-to-many relationship.

I'm Trying to get collection of garments with the properties.

$g= garment::where('is_active',1)->with(['properties'])->select('garments.id','garments.name as name','garments.display','garments.is_new')->get();

The query returns a collection of objects but some objects don't return their properties. They all should have properties.

I can't figure out why there are a handful of garments that don't have their properties attached.

Note: I can do the following and it works as intended for all objects, so I know the relation exists.

garment::where('id',7)->properties();

0 likes
8 replies
MichalOravec's avatar

When you try

$garments = Garment::with('properties')->where('is_active', true)->get();

What do you get?

rubu710's avatar

I get a collection of garments, with an array of properties in the 'relations' for each garment object, except for about 8 objects. Those eight have an empty properties array.

MichalOravec's avatar

So that 8 garments don't have any properties in your database.

rubu710's avatar

They do have properties. That's what is confusing me.

When I check debugbar, this is the query it's running.

select `properties`.*, `garment_property`.`garment_id` as `pivot_garment_id`, `garment_property`.`property_id` as `pivot_property_id` from `properties` inner join `garment_property` on `properties`.`id` = `garment_property`.`property_id` where `garment_property`.`garment_id` in (1, 8, 2, 0, 0, 0, 142, 1, 1, 2073, 0, 23, 2871757, 287, 32, 35776, 3, 3, 46, 47, 48740055, 4, 4, 0, 59, 59, 5, 5, 5, 644, 695, 6, 775, 0, 0, 7, 9223372036854775807, 83, 852, 8, 8, 8, 9223372036854775807, 9363, 96, 99, 9, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

Not sure where those values are coming from.

Snapey's avatar

when you dump a large collection, only the first few will have all their relationship properties displayed.

So, what are the ids in your garments table? It seems to be using the wrong column for its primary key?

rubu710's avatar

when you dump a large collection, only the first few will have all their relationship properties displayed.

Yup, i just take the first 10 because I know that where one of the issues lie.

So, what are the ids in your garments table?

I'm using uuid.

rubu710's avatar

Turns out if I can remove the following:

...garment_property`.`garment_id` in (1, 8, 2, 0, 0, 0, 142, 1, 1, 2073, 0, 23, 2871757, 287, 32, 35776, 3, 3, 46, 47, 48740055, 4, 4, 0, 59, 59, 5, 5, 5, 644, 695, 6, 775, 0, 0, 7, 9223372036854775807, 83, 852, 8, 8, 8, 9223372036854775807, 9363, 96, 99, 9, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)...

it works as intended. Can I assume something is wrong with query builder then?

rubu710's avatar
rubu710
OP
Best Answer
Level 3

SInce I was using uuid's for the tables, the model required:

protected $keyType = 'string';

It was converting the uuid to int somewhere.

Please or to participate in this conversation.