Error in dates in laravel 5.5 and SQLServer

1

Inserting any date in laravel 5.5 into a SQLserver database gives me the following error:

SQLSTATE[22003]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]
Error de desbordamiento aritmético al convertir expression al tipo de datos datetime.

Apparently it is because laravel sends the date with the format 'Y-m-d H: i: s.000' (2017-10-24 13: 22: 35.000)

So far so good the problem is that before executing this query laravel sends the following instructions to the database:

-- network protocol: TCP/IP
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language Español
**set dateformat dmy**
set datefirst 1
set transaction isolation level read committed

set dateformat dmy means that you should receive 'd-m-Y H: i: s.000' but I do not know how to change the configuration so that the instructions and querys are consistent.

How can I correct this error?

    
asked by Renato Tapia 24.10.2017 в 18:45
source

2 answers

0

Finally solve the issue by putting the unconventional date format in each model ...

protected $dateFormat = 'd-m-Y H:i:s';

now the theme of the pasword_reset fix it by modifying the following in the local code of laravel:

vendor\laravel\framework\src\Illuminate\Auth\Passwords\DatabaseTokenRepository.php
protected function getPayload($email, $token)
    {
        return [
            'email' => $email, 'token' => $this->hasher->make($token),
            'created_at' => Carbon::now()->format('d-m-Y H:i:s')
        ];
    }

Although the most correct answer that could be carried out is the one of taylor: link

I do not know if someone could guide me through this process to be able to carry it out ... I formulated this question on the following page: Modify connection to SQL Server from Laravel 5.5 to manipulate dates

    
answered by 25.10.2017 / 17:38
source
1

You do not put an example of your code, but you can format the date with this line of code:

echo Carbon::createFromFormat('d-m-Y H', '1975-05-21 22')->toDateTimeString(); 

or

with this other line of code:

date("d-m-Y H", strtotime('1975-05-21 22'));

And send the format as expected by the server.

Reviewing the laravel documentation the model has a setDateFormat method maybe that will help you.

link

Reviewing the code the dateFormat is defined in getDateFormat of the file:

src / Illuminate / Database / Query / Grammars / SqlServerGrammar.php

Maybe you can do an extended and use that Grammars

    
answered by 24.10.2017 в 20:46