Hi there,
I have a date field called device_issued_date.
Migration looks like so :
$table->date('device_issued_date')->nullable(); //when was the device issued
I have an HTML input date field that submits the value in the format of d/m/Y (or dd/mm/yyyy) eg: 01/15/2025.
I'm trying to add set / get properties to the model so it will set that as a carbon date to the database but pull it when populating the form date field in the proper format.
I believe MySQL is expecting Y-m-d'
I wrote a little testing route
$device = Device::find(1);
dd($device->device_issued_date);
A plain dd shows the date as it is saved in the DB
On my model: This does not fire (testing with a log debug)
public function getDeviceIssueDateAttribute($value)
{
Log::debug('is this running');
return \Carbon\Carbon::parse($value)->format('d/m/Y');
}
note: I tested the name property and they do fire and mutate.
I have also tried this approach: It doesn't fire
protected function deviceIssueDate(): Attribute
{
Log::debug('foo');
return Attribute::make(
get: fn (date $value) => 'this date',
get: fn (string $value) => Carbon::createFromFormat('Y-m-d', $value)->format('d/m/Y'),
set: fn (string $value) => Carbon::parse($value)->format('mm/dd/yyyy'),
);
}
I have also tried to cast the property as a date. That gets all carbony which may be what I need to do.
What am I missing for an easy way to set a date from an HTML input into MySQL.
I can most likely do this at the controller level but I was hoping to leave it in the model in case this property is used in the future on another form.
- also please forgive me, the last big Laravel project I built was Laravel 7 and I'm just now making my comeback :) I missed it.