Skip to content

Eloquent tries to fetch relationship for non persisted model instances. #24241

Closed
@jonnsl

Description

@jonnsl
  • Laravel Version: 5.5.40 & 5.6.22
  • PHP Version: PHP 7.2.4
  • Database Driver & Version: Any

Description:

Eloquent will try to fetch relationship even if model isn't persisted yet.

Steps To Reproduce:

Given the Models:

Class A extends Model{}
Class B extends Model
{
    public function a()
    {
        return $this->hasOne(A::class)->withDefault();
    }
}

Trying to access/load the relationship 'a':

(new B)->getRelationValue('a');

Eloquent will try to fetch the related model with this query:

select * from `a_s` where `a_s`.`b_id` is null and `a_s`.`b_id` is not null

Which obviously will return nothing, so a default model will be generated and returned.

Expected Behavior

If the model is not persisted, no query should be issued at all, and the default returned right away.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions