Description:
If a model A
defines two hasMany relationships on model B
, each with it's own constraints, and you attempt to eager load both of the relations, it breaks with SQLSTATE[HY093]: Invalid parameter number (SQL: select * from files where type in (1, 2, 3) and files.parent_id in (16) and files.deleted_at is null)
. The bindings are messed up: [1, 2, 3, 4]
.
Steps To Reproduce:
class File extends Model {}
class User extends Model {
public function files(): HasMany {
return $this->hasMany(File::class, 'parent_id')
->whereIn('type', [1, 2, 3]);
}
public function otherFiles(): HasMany {
return $this->hasMany(File::class, 'parent_id')
->where('type', 4);
}
}
\EloquentSerialize::unserialize(
\EloquentSerialize::serialize(
User::with(['files', 'otherFiles'])
)
)->get();
I've also set up a GitHub repo: https://github.com/oprypkhantc/laravel-has-many-bug/commit/ef2112d811f0a28071c58f7a5cfed4379759d7e4