F
Filamentβ€’15mo 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β€’15mo ago
Why doesn't it work? looks ok
KeyMe
KeyMeOPβ€’15mo 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β€’15mo ago
no, it is saying DeviceImeis doesn't have type on that collection which is being returned dd($record->DeviceImeis)
KeyMe
KeyMeOPβ€’15mo 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β€’15mo ago
Replace: return $state . '(' . $record->DeviceImeis->type . ')'; with dd($record->DeviceImeis); return $state . '(' . $record->DeviceImeis?->type . ')';
KeyMe
KeyMeOPβ€’15mo ago
No description
toeknee
toekneeβ€’15mo ago
I suspect some do not have a deviceImeis
KeyMe
KeyMeOPβ€’15mo ago
hmm
KeyMe
KeyMeOPβ€’15mo ago
output, 2021468386 is supposed to be Addon:
No description
KeyMe
KeyMeOPβ€’14mo ago
nope
KeyMe
KeyMeOPβ€’14mo ago
sorry for late reply, just got to test it out, worked as u said. Thanks alot friend πŸ˜„
Want results from more Discord servers?
Add your server