freehunter freehunter - 7 months ago 37
MySQL Question

Laravel 5.3, Can't seem to configure which database to use?

I'm new to Laravel, just trying out some tutorials right now. I'm to the point of trying to connect to a database that I have configured (and am connected to that database using both Sequel Pro and the MySQL command line application). I know my database is working.

I've also configured Laravel to see this database in the /config/database.php file:

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

This is the code I'm using to connect to the database and pull out the record that matches id=1:

class Controller extends BaseController {
public function home() {

$user = \App\User::find(1);
echo '<pre>', print_r($user), '</pre>';

return view('home');

But when I run my application, I see the error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.users' doesn't exist (SQL: select * from
= 1 limit 1)

But if I actually create the database "homestead" with a table called "users", the code runs just fine.

Where am I missing a configuration option?


The issue is with config/database.php. env('DB_DATABASE', 'auth') this will first check whether DB_DATABASE is defined in .env file, if not exists only it will use auth database.

So you can update the .env file or update the config to not to use env variable. Like 'database' => 'auth'.