0

i want to connect to database using database.php file, but it's not works. I do not have the .env file. I have to configure something more, beyond the file database.php

file database.php:

<?php
return [

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

 'connections' => [

    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'my_database'),
        'username' => env('DB_USERNAME', 'mydatabase'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,            
    ],
   ]
 ]
6
  • Just to be sure, does your file have the opening <?php ? Commented Apr 8, 2020 at 20:36
  • I checked and yes, is set Commented Apr 8, 2020 at 20:39
  • You may want to update your sample code, just so it's 100% clear. Also, it is helpful when posting a question to include the exact error message, what you did to trigger it, and (often) what you expected to happen instead. Have you tried adding an .env file with some viable values? Commented Apr 8, 2020 at 20:40
  • -Error: the server responded with a status of 500 (Internal Server Error) -I can't use .env file in this situation Commented Apr 8, 2020 at 20:42
  • I would put that error in your question, for clarity. Have you tried connecting to the MySQL database using the credentials (either the defaults, or the ones in your .env)? E.g. mysql -h localhost -umydatabase -p my_database ? I.e. are you sure you can connect to your db even without Laravel? Commented Apr 8, 2020 at 20:45

1 Answer 1

1

Some of you database connection values are set to take from env file. Since you are not using env, you have to set them in the database.php file manually.

return [

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

 'connections' => [

    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL', 'Enter your db url here'), <--
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'Your db name'), <--
        'username' => env('DB_USERNAME', 'Db user name'), <--
        'password' => env('DB_PASSWORD', 'Db password'), <--
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,            
    ],
   ]
 ]

Note: You might need to change these settings again for deployment. And you have serve it after changing the file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.