F
Filament4mo ago
Lucky0

deep relationship

I have 3 models User, Car, and Brand and the relationship is User > Car > Brand I created a UserResource and I want to fetch the brand name
Tables\Columns\TextColumn::make('car')
->formatStateUsing(fn(Car $car): string => $car->brand->name)
->sortable(),
Tables\Columns\TextColumn::make('car')
->formatStateUsing(fn(Car $car): string => $car->brand->name)
->sortable(),
with the code above i get "Attempt to read property "name" on null"
Solution:
solved it ```php Tables\Columns\TextColumn::make('car_id') ->formatStateUsing(...
Jump to solution
2 Replies
Solution
Lucky0
Lucky03mo ago
solved it
Tables\Columns\TextColumn::make('car_id')
->formatStateUsing(
function ($state, Car $car): string {
$query = $car->with('brand')->where('id', $state)->first();
return $query->brand->name . ' - ' . $query->name->name;
}
)
->sortable(),
Tables\Columns\TextColumn::make('car_id')
->formatStateUsing(
function ($state, Car $car): string {
$query = $car->with('brand')->where('id', $state)->first();
return $query->brand->name . ' - ' . $query->name->name;
}
)
->sortable(),
Lucky0
Lucky03mo ago
I'm wondering if there are any better solution than this.