Controller pulling database data from wrong place

Asked

Viewed 555 times

0

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

This is my file . env:

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

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

However when I do exactly the same operation inside a controller, the value is returned homestead, shouldn’t return that

Following as is the connection with 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 ends up creating the situation that the database runs on Tinker, but the controller doesn’t work, because it takes the data from the wrong connection.

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

  • I could post your code for a better analysis.

  • ok updated....

  • which version of the Laravel used?

  • I’m using the 5

  • You’re using the Homestead?

2 answers

1

Check your default connection in the database.php file, and see if it’s named after the connection you posted here, in this 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 part:

databases:
    - homestead
  • I did what you went through, take a look at the return of the debug (remembering what I went through there in the post): PDO->__Construct('mysql:host=localhost;dbname=Homestead', 'Homestead', 'secret', array('0', '2', '0', false, '0')) It is pulling this data from somewhere that is not the . env

  • well, you’re using Master right, I honestly am out of touch because I didn’t set it up here. I know you have a file called "Homestead.yaml" in some corner there, and I believe that is where the configuration that the controller is pulling.

  • Give a peek there or search on the net that you should find, I unfortunately can not say more than that rs.

  • thanks anyway

  • nothing, I edited my answer, I believe that is very much the problem.

0

I don’t know much about Laravel, I believe his 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 of Laravel, it’s just an example, I believe you can change the names easily 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 only have one database then you can edit your file .env, note that there are more than one file .env, sane:

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

You must change the .env the others not, because they are just examples, no . 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 only have one bank, if the file . env doesn’t exist yet then read below:

Setting up the Laravel

  • Before using the 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 called .env.example copy it and the name of .env if it does not exist.

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

    APP_KEY=2XXjEeJYr2jO0lboHZPHLuN6eYmnxPvl
    
  • I recommend you use the command key:generate to generate such key, note that to use the command artesian it is necessary to have configured by Poser and have added the environment variables, navigate to the folder of your project and then use the command:

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

O . env in Laravel in development environment:

APP_ENV=local
APP_DEBUG=true
APP_KEY=2XXjEeJYr2jO0lboHZPHLuN6eYmnxPvl

O . env in Laravel in production environment:

APP_ENV=production
APP_DEBUG=false
APP_KEY=2XXjEeJYr2jO0lboHZPHLuN6eYmnxPvl
  • i would not like to directly edit the database setup file, I wanted it to grab the connection data from . env, but he’s picking up from I don’t know where. I went through .env.example and it also didn’t cause any effect... I have no idea where the Variable is taking the data from the connection to the database, since I changed all the files . env

  • @Adrianoluz But that’s what I said, don’t touch the .env.example and .env.production, they are examples only, the correct file is only .env, if it does not yet exist, then read this: http://answall.com/a/91799/3635 (where written Configuring the Laravel) :)

  • it exists and is yes, when having tested the database by Tinker it finds from the data . env, however inside the controler it pulls the data from elsewhere

  • @Adrianoluz I put that only as an explanation of how to create the . env chance does not exist, you should read the entire answer, I also posted how to edit the . env, you should check the content of it. Open the folder of your project and edit the file . env with sublimetext and see the contents.

  • Look at something curious, I went there in . env and created custom constants MY_DB, MY_USER, MY_PASS, with the values laravel_blog/root/blank. Then I went to the config/database and made the changes in env(), ok found, it’s running. But I still have no idea what is changing the values of the constants DB_DATABASE/DB_USERNAME/DB_PASSWORD to respectively Homestead/Homestead/secret, when running the controller.

  • @Adrianoluz but how are the values in the . env of DB_DATABASE/DB_USERNAME/DB_PASSWORD? Have you ever run the function env in some other file?

  • Yes it works on Tinker, but when it’s in a controller it finds other values for those constants. No . env is set to laravel_blog/root/blank, but when run it pulls Homestead/Homestead/secret

  • @Adrianoluz, please answer the question, I ask you one thing and you are saying something that is already clear in your question. I’ll be honest I think you’re getting confused. First on Tinker you shot env('DB_DATABASE','forge') instead of env('DB_DATABASE'), something else don’t you have to check if there is more than one project. Have you navigated to the correct folder by terminal? Using the vim in the same terminal or another text editor to open . env? There is a lot here that is not giving to understand what you did. I hope you’ll take it as a constructive criticism ;)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.