F
Filament7mo ago
Sydd

getRecordTitleAttribute from related model

Dear all, last time I have a lot of question. Please how can I create record title attibute from realated mode? for getGloballySearchableAttributes it works fine, but for getRecordTitleAttribute; in the Filament docs I read that it's must be a column / attributefrom model ... but is possible to get from related model ?
public static function getRecordTitleAttribute(): ?string
{
return 'user.fullName'; //not working
}

public static function getGloballySearchableAttributes(): array
{
return ['user.fullName']; // working fine
}
public static function getRecordTitleAttribute(): ?string
{
return 'user.fullName'; //not working
}

public static function getGloballySearchableAttributes(): array
{
return ['user.fullName']; // working fine
}
Solution:
This would go on your edit and view page classes. Not on the resource. ```php use Illuminate\Contracts\Support\Htmlable;...
Jump to solution
11 Replies
Povilas K
Povilas K7mo ago
Have you tried
public static function getGlobalSearchResultTitle(Model $record): string | Htmlable
{
return $record->user->fullName;
}
public static function getGlobalSearchResultTitle(Model $record): string | Htmlable
{
return $record->user->fullName;
}
Or something from the docs here: https://filamentphp.com/docs/3.x/panels/resources/global-search#adding-extra-details-to-global-search-results
Sydd
SyddOP7mo ago
Thanks for answer, function getGlobalSearchResultTitle works fine, but i want by this way set the getRecordTitleAttribute ... i try:
public static function getRecordTitleAttribute(): ?string
{
return 'user.fullName'; //not working
}
public static function getRecordTitleAttribute(): ?string
{
return 'user.fullName'; //not working
}
public static function getRecordTitleAttribute(): ?string
{
return $record->user->fullName; //not working
}
public static function getRecordTitleAttribute(): ?string
{
return $record->user->fullName; //not working
}
btw thanks for your YTB chanel 👍
krekas
krekas7mo ago
any errors?
Sydd
SyddOP7mo ago
public static function getRecordTitleAttribute(): ?string
{
return 'user.fullName';
}
public static function getRecordTitleAttribute(): ?string
{
return 'user.fullName';
}
nothing happend.
public static function getRecordTitleAttribute(): ?string
{
return $record->user->fullName;
}
public static function getRecordTitleAttribute(): ?string
{
return $record->user->fullName;
}
Undefined variable $record
krekas
krekas7mo ago
try $this->record
Sydd
SyddOP7mo ago
error => Using $this when not in object context
krekas
krekas7mo ago
don't use this method this method only gets the $recordTitleAttribute property what's wrong with the getGlobalSearchResultTitle?
awcodes
awcodes7mo ago
Are you trying to display the table column differently? What exactly are you trying to change on the page?
Sydd
SyddOP7mo ago
want to replace "Driver card" with fullName of user who is related to to driver card, tha sme in the infolist / edipage for driver card. I do it for Vehicles, but in this case i use
protected static ?string $recordTitleAttribute = 'vehicleName'
protected static ?string $recordTitleAttribute = 'vehicleName'
where vehicleName is column id db / model attribute. So... is possible pass relation 'user.fullName' to tittle attribute ? If no, probably i will create collumn in driver card with user fullname and immediately update it of user change name ...
No description
No description
Solution
awcodes
awcodes7mo ago
This would go on your edit and view page classes. Not on the resource.
use Illuminate\Contracts\Support\Htmlable;

public function getTitle(): string | Htmlable
{
return $this->record->user->fullName;
}
use Illuminate\Contracts\Support\Htmlable;

public function getTitle(): string | Htmlable
{
return $this->record->user->fullName;
}
Sydd
SyddOP7mo ago
Thanks !
Want results from more Discord servers?
Add your server