[6.x]: Lazy eager loading returns nothing for a multi-instance relation field once its value was read
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
Start in BaseRelationField::getEagerLoadingMap() and compare how it reads the query conditions with normalizeValueForInput(), which uses getWhereForColumn('elements.id') and the values key. Check how the normalized value's whereIn() condition is represented, then run or add coverage for the multi-instance Assets field scenario. Done means lazy eager loading returns the asset for an already-read field on entries in a multi-entry result set.
Written by the indexing model from the issue text.
Description
What happened?
An entry type has one Assets field placed twice in its layout (image, and logoOverHero as a second instance with its own handle). In a site template, {{ element.image.one() }} returns null when element came from a result set of two or more entries (e.g. the nested entries of a Matrix field), although the entry has an image (element.image.ids() returns it, and so does element.image.eagerly(false).one()).
BaseRelationField::getEagerLoadingMap() reads the target IDs from a normalized value's elements.id where with $where['value'], but normalizeValue() adds that constraint with whereIn(), which Laravel stores under values (getQuery()->wheres holds ['type' => 'In', 'column' => 'elements.id', 'values' => [...]]). So the IDs of every source element whose field was already normalized are lost. For single-instance fields fetchRelationsFromDbTable() covers this by querying the relations table, which hides the bug; for multi-instance fields it returns false by design, so the map comes back empty and the element gets an empty eager-loaded result. The element that triggers lazy eager loading always has a normalized value (reading the field is what runs the query), so it always loses its own relations.
The same file reads the same where correctly in normalizeValueForInput() ($value->getWhereForColumn('elements.id') and $where['values']).
Steps to reproduce
- Create an Assets field and add it twice to an entry type's field layout (second instance with a different handle).
- Add two or more entries of that type to a Matrix field on a page; give the first one an image in the first instance.
- In the page's site template:
{% for block in entry.myMatrix.all() %}{{ block.image ? block.image.one().id ?? 'null' }}{% endfor %}.
Expected behavior
The asset ID, as with block.image.eagerly(false).one() or with a single-instance field.
Actual behavior
null (block.image.all() is empty). Changing $where['value'] to $where['values'] in BaseRelationField::getEagerLoadingMap() fixes it.
Craft CMS version
6.x-dev (603f0e0, 2026-10-31)
PHP version
8.5
Operating system and version
macOS (Laravel Herd)
Database type and version
MySQL 8
Image driver and version
—
Installed plugins and versions
None needed to reproduce.
- Dominant language
- PHP
- Stars
- 3.6k
- Forks
- 712
- Avg merge
- 13h 5m
- Merged PRs (30d)
- 328
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from craftcms/cms
-
bug repo:cms
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
craftcms/cms#20248 · 1 comment ·
Maintainers usually reply within 1 day
-
bug repo:cms
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
craftcms/cms#20205 · 1 comment ·
Maintainers usually reply within 1 day
-
bug repo:cms
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
craftcms/cms#20203 · 1 comment ·
Maintainers usually reply within 1 day
-
[5.x]: Moving an asset with “Replace it” throws TypeError: volumeUid must be of type string, null givenPossibly taken @i-just claimed this 5 days ago. Openbug repo:cms
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
craftcms/cms#20128 · 2 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
bug repo:cms
Difficulty 4/5 1-2 days Newbie friendliness 55/100
craftcms/cms#20226 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
jwage/phpamqplib-messenger#163 ·
-
Newsletter subscription page logs Undefined array key "nw_error" on every GETPossibly taken @Codencode claimed this today. Open
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
PrestaShop/ps_emailsubscription#145 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
-
jira-created
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 2 days
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 78/100