Hello, I'm running into an issue while trying to build a dynamic query for a relationship.
For a bit of context, I'm using Spatie Query Builder and want to create a custom Sort that allows to sort by nested relationships and working as well with hasOne()->ofMany() or things like that.
I could almost get it to work but as soon as I call a more complexe relationship, I get a wrong query result containing where `my_table`.`user_id` is null and `my_table`.`user_id` is not null.
I could isolate the problem but don't know how to resolve it and it's probably caused by a native behavior of the query builder.
here's how to reproduce :
have 2 Models like that :
class User extends Authenticatable
{
...
public function active_contract()
{
return $this->hasOne(UserContract::class, 'user_id', 'id')->ofMany(['arrival_date' => 'max']);
}
class UserContract extends Model
{
protected $fillable = [
...
'arrival_date',
'user_id',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
and just call
$contract = User::active()->getModel()->active_contract();
dd($contract, $contract->toSql());
you should see something like that in the "query" property :
+wheres: array:3 [▼
0 => array:3 [▼
"type" => "Null"
"column" => "my_table.user_id"
"boolean" => "and"
]
1 => array:3 [▼
"type" => "NotNull"
"column" => "my_table.user_id"
"boolean" => "and"
]
2 => array:3 [▼
"type" => "raw"
"sql" => "´id´ = ´users.id´"
"boolean" => "and"
]
]
Can someone help me with this please?