Attempt to read property "model" on null.

I have a table that has a column "Briefcase" to show if the record (Business) belongs to a Briefcase. I'm implementing "->tooltip" to show to which User's Briefcase the Business belongs to.
Tables\Columns\TextColumn::make('briefcase.user.name')
->label('Briefcase')
->tooltip(fn (Business $record): string => "Business in " . $record->briefcase->user->name . "'s Briefcase"),
Tables\Columns\TextColumn::make('briefcase.user.name')
->label('Briefcase')
->tooltip(fn (Business $record): string => "Business in " . $record->briefcase->user->name . "'s Briefcase"),
The above "tooltip" code fails when one business is not allocated to any Briefcase, I get:
Attempt to read property "user" on null
Attempt to read property "user" on null
Please, any ideas how can I fix it? Thanks.
6 Replies
Dennis Koch
Dennis Koch2y ago
Use the nullsafe operator $record->briefcase?->user->name
LeandroFerreira
or data_get($record, 'briefcase.user.name') https://laravel.com/docs/10.x/helpers#method-data-get
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Pablo Torres
Pablo TorresOP2y ago
Nice, thank you for your help... Works. I have also tried your solution, also works. Now, I think this is better long time/maintenance. Also the "data_get" function accepts a default value which is nice and helpful. Thank you.
LeandroFerreira
yep
Dennis Koch
Dennis Koch2y ago
You can pass a default via ?? 'default' $record->briefcase?->user->name ?? 'default' Embrace modern PHP 😅
Pablo Torres
Pablo TorresOP2y ago
Honestly, this tool/chat and of curse FilamentPHP is pushing me to learn more and more... thanks.
Want results from more Discord servers?
Add your server