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

DKP | Jan's avatar

Showing HasMany field using TextArea at index page Nova

Hi,

I want to show a HasMany field at the index page. I have found a solution by doing it using Textarea. See my code how it is possible to show the HasMany Student Log field on the index page using the Textarea Student Log field:

/**
 * Get the fields displayed by the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function fields(Request $request)
{
    return [
        ID::make(__('ID'), 'id')->sortable()->hideFromIndex(),

        Text::make('First Name')->required()->sortable(),
        Text::make('Last Name')->required()->sortable(),
        
        Text::make('Student Schoolsystem ID')->sortable(),
        
        Date::make('Date of birth')->format('DD-MM-YYYY')->sortable(),

        new Panel('Support details', $this->supportDetails()),

        HasMany::make('Student log', 'student_logs', StudentLog::class)->nullable()->sortable(),
        Textarea::make('Student log', 'student_logs')->hideWhenCreating()->hideWhenUpdating()->showOnIndex(),

        BelongsTo::make('Author', 'author', User::class)->default($request->user()->id)->sortable()->hideFromIndex(),


    ];
}

I have hide the HasMany from the index page, and the Textarea field from the creating and editing page. So, for editing and creating I am using the HasMany, and to make it visible at the index, I'm using the Textarea field.

This: Textarea::make('Student log', 'student_logs')->hideWhenCreating()->hideWhenUpdating()->showOnIndex(),

is giving: [{"id":2,"student_log":"test2","user_id":1,"student_id":1,"created_at":"2022-05-13T12:25:36.000000Z","updated_at":"2022-05-13T12:25:36.000000Z"}] as result. What I want only to show is the student_log text, so in this result it have to be only test2.

Is there someone who have a solution to do this? To get this done for BelongsToMany fields I have used this package: https://github.com/lathanhvien/custom-belongs-to-many-field

Thanks in advance!

0 likes
3 replies
wingly's avatar
wingly
Best Answer
Level 29

You can use computed fields for this by default would be hidden on the forms too.

Textarea::make('Student log',  function () {
	return $this->student_logs->pluck('student_log')->implode(', ');
})->showOnIndex(),
1 like

Please or to participate in this conversation.