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

darkec's avatar

Polymorphic relation returns query like "Is null and is not null"

I created polymorphic relation and it works just fine, but when I query it I get query like this one:

select * from `storage_loads` 
where `storage_loads`.`storage_id` is null and `storage_loads`.`storage_id` is not null and `storage_loads`.`storage_type` = ?

The problem is this part

where `storage_loads`.`storage_id` is null and `storage_loads`.`storage_id` is not null 

This is my relation inside HomeStorages model

    public function storageLoads()
    {
        return $this->morphMany( 'StorageLoads\Model', 'storage');
    }

This is my query

$this->homeStorages->storageLoads()->toSql();

Why Laravel adds "is null and is not null"

0 likes
1 reply
darkec's avatar
darkec
OP
Best Answer
Level 1

The problem was with this:

$this->homeStorages->storageLoads()->toSql();

I was taking all records and relations to them, but when I do this:

$this->homeStorages->find(1)->storageLoads()->toSql();

It works just fine

Please or to participate in this conversation.