#Cannot eager load nested relationship

2 messages · Page 1 of 1 (latest)

solemn dragon
#

I am trying to eager load a relationship but for some reason it just returns null. For all my queries the model relationship references the job model

public function model()
{
    return $this->belongsTo(Job::class, 'job', 'JOB');
}

If I run this query I get the part

App\Models\ActiveJob::query()->with('model')->first()->model->part;

but if I run this, part is null

App\Models\ActiveJob::query()->with('model.part')->first()->model->part;

I added database logging to my AppServiceProvider to see what queries are being ran and I was able to verify that the queries returned the correct data

[2022-10-13 12:07:45] local.INFO: select top 1 * from [job_active]  
[2022-10-13 12:07:45] local.INFO: select * from [dbo].[SF_JOB] where [dbo].[SF_JOB].[JOB] in (?) ["*redacted*"] 
[2022-10-13 12:07:45] local.INFO: select * from [dbo].[SF_ITEM] where [dbo].[SF_ITEM].[ITEM] in (?) ["*redacted*"] 

Here is the part relationship on the job model

public function part()
{
    return $this->hasOne(Item::class, 'ITEM', 'ITEM');
}
orchid root
#
public function part()
{
    return $this->hasOne(Item::class, 'ITEM', 'ITEM');
}