Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

audetcameron@gmail.com's avatar

Laravel 11 Accessor / Mutator on a date via model and html input - help

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.
0 likes
5 replies
tykus's avatar

The property you access is device_issued_date (note issued), but the two attempted Accessors you implemented result in a property called device_issue_date (note issue)! So, the Accessor methods never get executed unless you use:

dd($device->device_issue_date);
2 likes
audetcameron@gmail.com's avatar

@tykus Thank you kindly. That was 100% it. I may have just needed more coffee lol <3 I appreciate you taking the time to look at it. I owe you a sanity check in the future :)

tykus's avatar

@audetcameron@gmail.com no worries, feel free to mark the thread solved if you're all set

Sinnbeck's avatar

I would also recommend looking into why your form is submitting in that format. I dont think that is how it is supposed to work

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

Note: The displayed date format will differ from the actual value — the displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd.

LaryAI's avatar
Level 58

To handle date formatting in Laravel models using accessors and mutators, you can use the Attribute class to define custom logic for getting and setting attributes. Here's how you can achieve this for your device_issued_date field:

  1. Ensure the Date is Cast Correctly: First, make sure your date field is cast to a date in your model. This will help Laravel automatically handle date conversions.

  2. Define Accessor and Mutator: Use the Attribute class to define how the date should be formatted when retrieved and stored.

Here's how you can implement this in your model:

use Illuminate\Database\Eloquent\Casts\Attribute;
use Carbon\Carbon;

class Device extends Model
{
    protected $casts = [
        'device_issued_date' => 'date',
    ];

    protected function deviceIssuedDate(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => Carbon::parse($value)->format('d/m/Y'),
            set: fn ($value) => Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d')
        );
    }
}

Explanation:

  • Casting: By casting device_issued_date to date, Laravel will automatically convert it to a Carbon instance when you retrieve it from the database.

  • Accessor (get): The get function formats the date from the database (Y-m-d) to the desired format (d/m/Y) when you access it.

  • Mutator (set): The set function takes the date input from the form (d/m/Y) and converts it to the format expected by the database (Y-m-d) before saving.

Additional Tips:

  • Debugging: If your accessor or mutator isn't firing, ensure that the method names match the attribute name in camelCase. Also, check for any typos or case sensitivity issues.

  • Testing: Use dd() or Log::debug() to test if your accessor and mutator are being called correctly.

By following this approach, you can keep your date formatting logic within the model, making it reusable across different parts of your application.

Please or to participate in this conversation.