can anyone spot what i am doing wrong here?

I am trying to display a hyphen if there is no date added to the record this is my code
TextColumn::make('due_date')
->sortable()
->date()
->toggleable()
->color(function ($record) {
$value = $record->due_date;

if ($value === null) {
return $value;
}

$date = \Carbon\Carbon::parse($value);
$now = \Carbon\Carbon::now();

$style = '';
if ($date->isPast()) {
$style = 'danger';
} elseif ($now->diffInWeeks($date) <= 4) {
$style = 'warning';
}
return $style;
})
->formatStateUsing(function ($value, $record) {
if ($value === null) {
return '-';
}
return $value;
}),
TextColumn::make('due_date')
->sortable()
->date()
->toggleable()
->color(function ($record) {
$value = $record->due_date;

if ($value === null) {
return $value;
}

$date = \Carbon\Carbon::parse($value);
$now = \Carbon\Carbon::now();

$style = '';
if ($date->isPast()) {
$style = 'danger';
} elseif ($now->diffInWeeks($date) <= 4) {
$style = 'warning';
}
return $style;
})
->formatStateUsing(function ($value, $record) {
if ($value === null) {
return '-';
}
return $value;
}),
Any ideas where i am going wrong ?
Solution:
You can try ->default('-')
Jump to solution
18 Replies
Solution
DysonSphere
DysonSphere10mo ago
You can try ->default('-')
nostrodamned
nostrodamned10mo ago
oh my gosh so easy! Thank you sire!
nostrodamned
nostrodamned10mo ago
actually spoke a litle fast - it worked initially, then i set a date, then removed it and now get this error Could not parse '-': Failed to parse time string (-) at position 0 (-): Unexpected character @DysonSphere
DysonSphere
DysonSphere10mo ago
Where did you put the ->default('-')?
nostrodamned
nostrodamned10mo ago
TextColumn::make('due_date')
->sortable()
->date()
->default('-')
->color(function ($record) {
$value = $record->due_date;
ray($record);
if ($value === null) {
return $value;
}

$date = \Carbon\Carbon::parse($value);
$now = \Carbon\Carbon::now();

$style = '';
if ($date->isPast()) {
$style = 'danger';
} elseif ($now->diffInWeeks($date) <= 4) {
$style = 'warning';
}
return $style;
}),
TextColumn::make('due_date')
->sortable()
->date()
->default('-')
->color(function ($record) {
$value = $record->due_date;
ray($record);
if ($value === null) {
return $value;
}

$date = \Carbon\Carbon::parse($value);
$now = \Carbon\Carbon::now();

$style = '';
if ($date->isPast()) {
$style = 'danger';
} elseif ($now->diffInWeeks($date) <= 4) {
$style = 'warning';
}
return $style;
}),
DysonSphere
DysonSphere10mo ago
If you remove the ->date(), there will be no error
nostrodamned
nostrodamned10mo ago
yes but it wont format the date then on the table when there is a value
awcodes
awcodes10mo ago
You were on the right track to start with. Change ‘$value’ to ‘$state’ in the formatStateUsing() modifier.
nostrodamned
nostrodamned10mo ago
sorry for being a bit thick here!! you mean like
TextColumn::make('due_date')
->sortable()
->date()
->toggleable()
->color(function ($record) {
$value = $record->due_date;

if ($value === null) {
return $value;
}

$date = \Carbon\Carbon::parse($value);
$now = \Carbon\Carbon::now();

$style = '';
if ($date->isPast()) {
$style = 'danger';
} elseif ($now->diffInWeeks($date) <= 4) {
$style = 'warning';
}
return $style;
})
->formatStateUsing(function ($state, $record) {
if ($state === null) {
return '-';
}
return $state;
}),
TextColumn::make('due_date')
->sortable()
->date()
->toggleable()
->color(function ($record) {
$value = $record->due_date;

if ($value === null) {
return $value;
}

$date = \Carbon\Carbon::parse($value);
$now = \Carbon\Carbon::now();

$style = '';
if ($date->isPast()) {
$style = 'danger';
} elseif ($now->diffInWeeks($date) <= 4) {
$style = 'warning';
}
return $style;
})
->formatStateUsing(function ($state, $record) {
if ($state === null) {
return '-';
}
return $state;
}),
- it doesnt work
awcodes
awcodes10mo ago
Can you dd($state) it may not be null.
nostrodamned
nostrodamned10mo ago
it doesnt seem to even hit the formatStateUsing method when i add the dd in their - the record column for due_date in teh db is NULL though
nostrodamned
nostrodamned10mo ago
this is a gist https://gist.github.com/craigvc/a0d1892d400568292f46d56dde8d0b65 Have i done something wrong?
Gist
TaskResource.php
GitHub Gist: instantly share code, notes, and snippets.
awcodes
awcodes10mo ago
give me a minute. gonna check on something. i would expect it to work. hmm. not sure why formatStateUsing isn't getting called when the initial value is null either way, even if you fake it with getStateUsing() you're going to have a problem because the ->date() modifier will break trying to parse '-' into a Carbon instance. I think the only way your going to be able to do this to not use ->date() and return your own formatted string for it.
Tables\Columns\TextColumn::make('due_date')
->label('Due Date')
->default('')
->formatStateUsing(function ($record) {
return $record->due_date?->toDateTimeString() ?? '-';
}),
Tables\Columns\TextColumn::make('due_date')
->label('Due Date')
->default('')
->formatStateUsing(function ($record) {
return $record->due_date?->toDateTimeString() ?? '-';
}),
nostrodamned
nostrodamned10mo ago
I am getting Call to a member function toDateTimeString() on string with that (though in my model i have protected $dates = ['due_date', 'start_date', 'completed_date']; ) so it should be a date?
awcodes
awcodes10mo ago
it should be cast to a carbon instance. it shouldn't be a string that code works in the Filament demo
nostrodamned
nostrodamned10mo ago
so on the form the date is added DatePicker::make('due_date')->prefix('Due Date')->label('')->native(false)->columnSpan(3), In the Db its stored as datetime I am a bit stumped where i am wrong here?
awcodes
awcodes10mo ago
maybe trying casting them directly instead of using the $dates property.
protected $casts = ['due_date' => 'datetime']
protected $casts = ['due_date' => 'datetime']
nostrodamned
nostrodamned10mo ago
That was it ! I tweaked a little so we can pull the user date format later to this
->formatStateUsing(function ($record) { if ($record->due_date !== null) { return $record->due_date->format('Y-m-d'); } return '-'; }) Which seems to work! Thanks for your help!!!!