F
Filamentβ€’10mo ago
KeyMe

Listing multiple values for a relationship with another attribute

I have a vehicle relation manager table where i want to display every IMEI numbers associated with the vehicle. On top of that, I want to format the imei list to include its' type as below.
"0185701241241 (Device)
015830914194 (Addon)
"0185701241241 (Device)
015830914194 (Addon)
Current code, obviously doesnt work:
Tables\Columns\TextColumn::make('deviceimeis.imei')
->formatStateUsing(function ($state, Vehicle $record) {
return $state . '(' . $record->DeviceImeis->type . ')';
})
->label('IMEI')
->listWithLineBreaks()
->bulleted(),
Tables\Columns\TextColumn::make('deviceimeis.imei')
->formatStateUsing(function ($state, Vehicle $record) {
return $state . '(' . $record->DeviceImeis->type . ')';
})
->label('IMEI')
->listWithLineBreaks()
->bulleted(),
11 Replies
toeknee
toekneeβ€’10mo ago
Why doesn't it work? looks ok
KeyMe
KeyMeβ€’10mo ago
this error appears Property [type] does not exist on this collection instance. I assume its because i didnt specify the array index..?
toeknee
toekneeβ€’10mo ago
no, it is saying DeviceImeis doesn't have type on that collection which is being returned dd($record->DeviceImeis)
KeyMe
KeyMeβ€’10mo ago
tried replacing with this, but every imei record will be same as the first record for ex "device"
foreach ($record->DeviceImeis as $rec) {
return $state . '(' . $rec->type . ')';
}
foreach ($record->DeviceImeis as $rec) {
return $state . '(' . $rec->type . ')';
}
toeknee
toekneeβ€’10mo ago
Replace: return $state . '(' . $record->DeviceImeis->type . ')'; with dd($record->DeviceImeis); return $state . '(' . $record->DeviceImeis?->type . ')';
KeyMe
KeyMeβ€’10mo ago
No description
toeknee
toekneeβ€’10mo ago
I suspect some do not have a deviceImeis
KeyMe
KeyMeβ€’10mo ago
hmm
KeyMe
KeyMeβ€’10mo ago
output, 2021468386 is supposed to be Addon:
No description
KeyMe
KeyMeβ€’10mo ago
nope
KeyMe
KeyMeβ€’10mo ago
sorry for late reply, just got to test it out, worked as u said. Thanks alot friend πŸ˜„