about uscontact

How to setting to use SQL Server in Laravel

Author: zemna
Date: April 17, 2021
Share this:

When I setup Laravel project for my company, I've faced the problem about SQL Server database connection problem. I've googled and now I can use it.

1. Install php7.0-sybase to Homestead

New version of Homestead uses php7.0 to serve laravel website. So, I installed php7.0-sybase using bellow command.

$ sudo apt-get install php7.0-sybase

2. Edit /etc/freetds/freetds.conf file

I don't know about TDS but I followed reference bellow,

$ sudo vi /etc/freetds/freetds.conf

tds version = 7.2
client charset = UTF-8

Also create /etc/freetds/locales.conf file to allow correct parsing of dates.

$ sudo vi /etc/freetds/locales.conf

date format = {83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}Y-{83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}m-{83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}d {83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}I:{83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}M:{83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}S.{83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}z

3. add getDateFormat() function to each Model classes

Eventhough I already set the datetime format from above, I still got the Data missing error when access Carbon class. This is because the difference of datetime string format. My SQL Server gives '2016-06-20 13:00:00' datetime string to Laravel, but Carbon accepts 'Y-m-d H:i:s.000' format to create instance.

So, add getDateFormat() function to each Model classes to give format information.

protected function getDateFormat() {
return 'Y-m-d H:i:s+';

SqlServerGrammer.php file returns Y-m-d H:i:s.000 DateFormat. Change it to Y-m-d H:i:s to apply all models or just delete this function. Because Grammer.php abstract class returns Y-m-d H:i:s format...

Share this:

Leave a Reply

Your email address will not be published.


Professional software development agency estalished in Indonesia. We offers website setup, web application development, fulfillment service, and etc.
Follow us
Follow us to get latest news and tutorials.
2022 - Copyright, All Rights Reserved, Made by ZEMNA.NET with ❤️