#BelongsToMany Full Name Accessor

22 messages · Page 1 of 1 (latest)

hasty coral
#

I have

    {
        return $this->belongsToMany(Course::class);
    }```
in Course Model have ```public function getFullNameAttribute()
    {
        return $this->department->name . ' ' . $this->course . ' ' . __('term');
    }```

When call Tables\Columns\TextColumn::make('courses.full_name')

Getting empty field.

How can I solve this?
rich irisBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

shut birch
#

This is because we use the column names in the query, not as an accessor.

do

Tables\Columns\TextColumn::make('courses.full_name')
  ->getStateUsing(fn($record) => $record->courses->full_name)
hasty coral
#

getting Property [full_name] does not exist on this collection instance.

shut birch
#

try

$record->courses->fullName

hasty coral
#

already tried with this:

                    ->formatStateUsing(fn(Model $state) => $state->full_name)```
hasty coral
hasty coral
#

Also I noted if working with belongsTo then Tables\Columns\TextColumn::make('course.full_name') working

#

virtual column much better as @open mirage described!

open mirage
hasty coral
#

I use searchable with array like searchable(['name', 'surname'])

#

Can you explain why course.full_name working, but courses.full_name not working. belongsTo vs belongsToMany

hasty coral
#

When call Tables\Columns\TextColumn::make('courses.full_name')

Getting empty field.

hasty coral
#

yes Tables\Columns\TextColumn::make('courses.course') working

#
        'department_id',
        'course',
    ];```
#
    {
        return $this->department->name . ' ' . $this->course . ' ' . __('term');
    }```
#

in Subject Model

open mirage