Problems saving data with LARAVEL 5.5


I have 2 methods to save and another to consult and I have them as follows:

 public function store(Request $request)

    $comment = new Comment();
    $comment->user_id = $request->user_id;
    $comment->factura_id = $request->factura_id;
    $comment->comment = $request->comment;

    if ( $request->ajax() ){
        return $comment;


public function show($id)
    $comments = Comment::select('*')->where('factura_id', $id)->with('user:id,name')->get();

    return $comments;

Everything worked fine but today 13-01-2017 when wanting to save it gives me an error it tells me:


SQLSTATE [22007]: [Microsoft] [ODBC Driver 13 for SQL Server] [SQL   Server] The conversion of nvarchar data type into datetime produced a   value out of range. (SQL: insert into [comments] ([user_id],   [invoice_id], [comment], [updated_at], [created_at]) values (1, 554,   commeny, 2018-01-13 14: 25: 44.015, 2018-01-13 14: 25: 44.015))

It does not let me save by the methods of created_at and updated_at, the easy solution is to remove them but I do not want them because I need them, and I do not know how to solve them. try to do it by adding a my model

protected $dateFormat = 'Ymd h:i:s';

and it works for me, I can save it but when I ask it, it says:

Unexpected data found. Unexpected data found. Trailing data

Some colleagues solution ???

asked by Carlos Agaton 13.01.2018 в 21:35

2 answers


The problem is that you are inserting a date that contains milliseconds, in the datetime format of sql that is "Y-m-d H:m:s" ,

The fastest solution is to manually insert the timestamps in the correct format

$comment->created_at = date('Y-m-d H:m:s');

$comment->updated_at = date('Y-m-d H:m:s');

answered by 14.01.2018 в 00:34

I recommend that you do another class and that all your models make extends of that new class. Ideally, you should overwrite the dateFormat function of Model.

use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model 

    public function getDateFormat()
        $database = env('DB_CONNECTION');

        $date_format = parent::getDateFormat();
        if ($database == 'sqlsrv') 
            $date_format = 'Ymd H:i:s';

        return $date_format;

class User extends BaseModel

So if you use SQL you should use the Ymd H: i: s format.

answered by 24.01.2018 в 16:38