I'm working on a project running on an existing database. The problem is that datetime columns are inserted using a wrong format in SQL Server. The server uses datetime as Y-m-d and data is being saved as Y-d-m.
I've made some tests, and when saving to MariaDB datetime is saved properly.
There are custom fields for updated_at and created_at, so they are declared on the model.
In the model
class NotaFaturamento extends Model
{
const CREATED_AT = 'DT_CADASTRO';
const UPDATED_AT = 'DT_ATUALIZACAO';
This is the QueryLog print after saving data. As you can see in the query log, datetime format is correctly parsed to SQL Server.
On Config\app.php
'timezone' => 'America/Sao_Paulo',
'locale' => 'pt-BR',
Is this something that needs to be configured on SQLServer? I've searched a lot about this, but most of the responses are regarding SQL Server separators.
I also declared protected $dateFormat = 'Y-m-j h:i:s:000A'; on the model but the same problem happens. The issue is also present when storing Carbon objects.
Regards.
EDIT
As pointed out by Dan, the issue could be the DATEFORMAT on the SQL Server being used as DMY. Also, as pointed this issue and answered by @dns_nx, there is a workaround to manually change dateformat for saving on SQL Server.
I've added to my model
public function getDateFormat()
{
return 'Y-d-m H:i:s.v';
}
And any other date attributes on the model should be declared as being date:
protected $dates = ['DT_EMISSAO', 'DT_COMPETENCIA'];
I don't think this is the proper way to solve the issue, but it does work. And you could create another basemodel as mentioned by @dns_nx.
Regards