Using Placeholder giving error.

I am getting error "Call to a member function toFormattedDateString() on null" . This is fine because my form is not submitted to create record yet. But I am not very good in Filamentphp. How to use below then. Any specific code I can make an entry so i dont get this error.
Placeholder::make('created')
->content(fn (CompanyAvailableForSalesProfileModel $record): string => $record->created_at->toFormattedDateString())
Placeholder::make('created')
->content(fn (CompanyAvailableForSalesProfileModel $record): string => $record->created_at->toFormattedDateString())
Solution:
Thanks @Ashk . I did minor change in your second code which was giving error. I used below
Placeholder::make('created')
->content(fn (?Model $record): string => $record?->created_at->toFormattedDateString() ?? '-')
Placeholder::make('created')
->content(fn (?Model $record): string => $record?->created_at->toFormattedDateString() ?? '-')
. And it is working like a charm. Thanks to you....
Jump to solution
2 Replies
Ashk
Ashk8mo ago
PS: I replace your fqcn Model to have something shorter in my exemple If you want completely hide the component
Placeholder::make('created')
->hidden(fn (?Model $record) => is_null($record))
->content(fn (?Model $record): string => $record?->created_at->toFormattedDateString())
Placeholder::make('created')
->hidden(fn (?Model $record) => is_null($record))
->content(fn (?Model $record): string => $record?->created_at->toFormattedDateString())
If you want a fallback
Placeholder::make('created')
->content(fn (?Model $record): string => $record?->created_at->toFormattedDateString() ?? '-')
Placeholder::make('created')
->content(fn (?Model $record): string => $record?->created_at->toFormattedDateString() ?? '-')
Solution
Pritbor
Pritbor8mo ago
Thanks @Ashk . I did minor change in your second code which was giving error. I used below
Placeholder::make('created')
->content(fn (?Model $record): string => $record?->created_at->toFormattedDateString() ?? '-')
Placeholder::make('created')
->content(fn (?Model $record): string => $record?->created_at->toFormattedDateString() ?? '-')
. And it is working like a charm. Thanks to you.
Want results from more Discord servers?
Add your server