rickyreza12's avatar

How to set up the database sql server on laravel?

i tried to connect database from my apps to sql server and it seems my configurations was right

database config


return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_OBJ,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'sqlsrv'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

** env **

APP_ENV=local
APP_KEY=base64:1uUEsUbcFAFa3dF8u6lPepioPenj1uP1t2gAs/ZrVeQ=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=sqlsrv
DB_HOST=172.16.20.179
DB_PORT=3306
DB_DATABASE=PROGRAMMING
DB_USERNAME=sa
DB_PASSWORD=controlroom

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_KEY=
PUSHER_SECRET=
PUSHER_APP_ID=

I think my configurations is good then, but i keep getting error

Database [sqlsrv2] not configured.

i know that there is no sqlsrv2 in there , because i use the sqlsrv2 first and i change all elloquent from this

class CrewProgram extends Model
{
    protected $connection = 'sqlsrv2';
    protected $table='transaction_crewprogrammemo';
}

into this

class CrewProgram extends Model
{
    protected $table='transaction_crewprogrammemo';
}

that's why i got that databas sqlsrv2 not configured error, i thought if i change into that i will be able to use the sqlsrv that my configurations is using that's why i delete my sqlsrv2, but if i change it like this it's work

class CrewProgram extends Model
{
    protected $connection = 'sqlsrv';
    protected $table='transaction_crewprogrammemo';
}

what should i do to make the eloquent didn't need to use connection = 'sqlrv' anymore, i tought configure the .env and config/database.php was enough, help me to solve this, i guess i was wrong, what should i do to use sqlsrv in there?

0 likes
8 replies
Prokosa's avatar
class CrewProgram extends Model
{
    protected $connection = 'sqlsrv2';
    protected $table='transaction_crewprogrammemo';
}

configured

DB_CONNECTION=sqlsrv

sqlsrv2!=sqlsrv

Snapey's avatar

Could you have missed changing it back somewhere?

Make sure your config is not cached with php artisan config:clear

rickyreza12's avatar

i want to use sqlsrv not sqlsrv2, because i already change my database into sqlsrv

rickyreza12's avatar

Yeah I'm sure, because i can use the sqlsrv if i'm using $connection = sqlsrv, I think the dp_port not the problem

jlrdw's avatar

I thought it was 1433, but it's your db you know your data the best.

Hissein_Tonio's avatar

for those who come after #just port to null value

DB_CONNECTION=sqlsrv

DB_HOST=172.16.20.179\your_instance_name or your_server_name

DB_PORT=null

DB_DATABASE=PROGRAMMING

DB_USERNAME=sa

DB_PASSWORD=controlroom

1 like

Please or to participate in this conversation.