Controller pulling bank data from the wrong place

0

I have the application properly configured and running normally. But one detail is stuck.

This is my .env file:

APP_ENV=local
APP_DEBUG=true
APP_KEY=9FaEj6iJCBur1favtWQ88b8m1anbGbzP
DB_HOST=localhost
DB_DATABASE=laravel_blog
DB_USERNAME=root
DB_PASSWORD=
...

When I run the command env('DB_DATABASE','forge') in tinker, the value laravel_blog is returned, as it should be.

However when I do exactly the same operation inside a controller, the value homestead is returned, it should not return this

Following is the connection to the bank:

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

It finishes creating the situation that the bank runs in the tinker, but in the controller it does not work, because it takes the wrong connection data.

The problem is that I have no idea where laravel is pulling the value of the constant inside the controller, since in .env the configured value is different.

    
asked by anonymous 22.12.2015 / 19:09

2 answers

1

Check your default connection in the database.php file, and see if you have the name of the connection you posted here, in the case " mysql "

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

Change these lines from your database.php

'host'      => env('DB_HOST'),
'database'  => env('DB_DATABASE'),
'username'  => env('DB_USERNAME'),
'password'  => env('DB_PASSWORD'),

Take a look at the file ~/.homestead/Homestead.yaml in the part:

databases:
    - homestead
    
22.12.2015 / 19:50
0

I do not understand much of Laravel, I believe your example:

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

Be the default for Laravel, just an example, I believe you can easily change names by editing /config/database.php :

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'laravel_blog',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

If you have only one database then you can edit your .env file, note that there are more than one .env file, they are:

  • .env , .env.example and .env.production

You should change the .env to the others, since they are only examples, in the .env there are these variables:

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

Just edit for something like in the example:

DB_HOST=localhost
DB_DATABASE=laravel_blog
DB_USERNAME=root
DB_PASSWORD=root

If you have only one database, if the .env file still does not exist then read the following:

Configuring Laravel

  • Before using Laravel as I said in this answer it is necessary to create the file .env , note that in the laravel folder there is a file named .env.example copy it and the name .env if it does not exist.

  • Then you will need to set APP_KEY , it must be a 32-character key, for example:

    APP_KEY=2XXjEeJYr2jO0lboHZPHLuN6eYmnxPvl
    
  • I recommend that you use the key:generate command to generate such a key, note that to use the artesian command you must have the composer set up and have added the artesian to the environment variables , navigate to the folder of your project and then use the command:

    $ cd /home/user/laravel
    $ php artisan key:generate
    
  • When you go up the project for production (pro server online) you should change the line APP_DEBUG=true to APP_DEBUG=false , this will turn off the errors that should only be displayed by the development environment and not the end user and also change APP_ENV=local to APP_ENV=production .

The .env in Laravel in development environment:

APP_ENV=local
APP_DEBUG=true
APP_KEY=2XXjEeJYr2jO0lboHZPHLuN6eYmnxPvl

The .env in Laravel in production environment:

APP_ENV=production
APP_DEBUG=false
APP_KEY=2XXjEeJYr2jO0lboHZPHLuN6eYmnxPvl
    
22.12.2015 / 19:50