Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel sqlsrv - unable to create timestamps()

Tags:

laravel

I am migrating an existing projekt in L5.1.x to a new server running with Server 2012 and an SQL Server 2012.

I have some problems when using the timestamps() fields for tables. When ill add in my Scheme:

$table->timestamps();

Ill get the created_at and updated_at column with type "datetime".

But with that new configuration when ill try to add a Model to this database i am always getting that error:

QueryException: Unable to convert an nvarchar Value to and datetime Value ....

When ill disable the timestamp fields in my Model, everything is working:

public $timestamps = false;

.. but of course no values for created_at and updated_at

I dont know what cause this problem - any ideas? i could manually set the created_at and updated_at field to "GETDATE()" - but i would like to use Laravels base functionality.

like image 493
derdida Avatar asked Feb 07 '23 17:02

derdida


1 Answers

Ok ill found the problem - Laravel creates a datetime with format of:

"Y-m-d H:i:s.u"

But the SQL Server wants the format of:

"Y-d-m H:i:s"

So when you update your model function with:

public function fromDateTime($value)
{
    return Carbon::parse(parent::fromDateTime($value))->format('Y-d-m H:i:s');
}

You are able to convert to date format to the correct value for your SQL Server ( dont know why its Y-d-m, its not my server ;) )

like image 165
derdida Avatar answered Feb 10 '23 09:02

derdida