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

AUGUSTINNoha's avatar

Tricky date getter/setters

Hello everybody,

I work in a financial company as dev. I introduced my teammates to laravel a year or so and everybody is enjoying it ! I still struggle with a date format on SQL Server databases (really old version) as my company has decided different write and read formats. And as you mind guess, no way to change this behavior from de DB perspective... Your help would be appreciated :)

On tables, problematic columns are datetimes BUT :

  • write action use a french format d/m/Y H:i:s (We do really good bread though...)
  • read action use regular format Y/m/d H:i:s

I mostly adress this by creating a CustomBaseModel extending Eloquent/Model.

Then create any datetime related setters (I don't need to override getters as they work fine with the provided database column format).

Well, this works fine but it feels like I have a bad/weak approach (work fine for very small projects with few models). It also stop working as soon as we use third party libraries extending from the Base Eloquent Model.

I wonder if someone can brind new ideas to solve this issue. Of course I don't think modifying vendors library is a good practice (I'm pretty sure it's not;)) but how can I override base Eloquent model without making my onw...

Is it possible to change/add methods to the base eloquent/model from a service provider ? Composer way ?

Any help would be appreciated here

Thank you all !

0 likes
2 replies
martinbean's avatar

@augustinnoha You shouldn’t be trying to specify formats when reading and writing dates from the database. You should just be setting properties to DateTime or Carbon instances, and then retrieving them as such:

class Foo extends Model
{
    protected $casts = [
        'some_column' => 'datetime',
    ];
}

When you retrieve a date/time value from a model, you should have something in your application layer that converts it to a “human” format. Carbon even has a method to do just that built in:

<p>Created: {{ $foo->created_at->toFormattedDateString() }}</p>

These formatting methods also support localization: https://carbon.nesbot.com/docs/#api-localization

AUGUSTINNoha's avatar

@martinbean Thank you for that quick answer ! Well I'm not trying to cast a DateTime but change the "normal" behavior of Illuminate\Database\Eloquent\Model.php class.

As created_at and updated_at are filled automatically when persisting data I encounter an SQL Server error trying to convert string (date) to an insertable format of Y/m/d H:i:s. Though the db expects the incoming format to be d/m/Y H:i:s

When using my own models, I can make them extend a custom model (with setters like setCreatedAtAttribute and setUpdatedAtAttribute) itself extending from Illuminate\Database\Eloquent\Model.php

This works fine but require too much file modification when using artisan create:model and it doesn't work at all when using third party vendors relying on caravel default model.

I know this behavior is not common (works perfectly fine using managed db like Mysql). Tech support would not change that db setting...

Please or to participate in this conversation.