laravel / laravel/nova-issues

displayUsing not called on BelongsTo field when value is null

Open
#6,900 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
557
Forks
33
PR merge metrics
No merged PRs in 30d

Description

  • Laravel Version: 12.20
  • Nova Version: 5.7.4
  • PHP Version: 8.2
Description:

The issue reported in https://github.com/laravel/nova-issues/issues/1901 was fixed for most Fields, but for BelongsTo fields it's not working, The displayUsing and resolveUsing methods differ between BelongsTo and the rest that extend Fields

Detailed steps to reproduce the issue on a fresh Nova installation:

example:

This does not work, displayUsing is never called when the value is null:

 BelongsTo::make('Vessel')
    ->sortable()
    ->searchable()
    ->rules('nullable', 'integer', 'exists:App\Models\Vessel,id')
    ->displayUsing(fn (?Vessel $vessel) => $vessel === null ? 'Global' : $vessel->id)
    ->hideFromIndex(fn (ResourceIndexRequest $request) => $request->viaRelationship())
    ->nullable()
    ->help('If not provided Role is global, i.e. available to be assigned any user on any vessels'),

but this does work, displayUsing is called whatever the value:

Text::make('test')->nullable()->displayUsing(fn (?string $test) => $test ?? 'Missing')

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue on a fresh Nova installation using the BelongsTo and Text examples in the report. Compare how displayUsing and resolveUsing handle null values for BelongsTo versus Text, then verify that the BelongsTo displayUsing callback is invoked for a null relationship and produces the expected display value.

Written by the indexing model from the issue text.

Assessment

Tech stack
laravel, php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.