Multiple laravel connection with mysql - laravel

I want to do two Laravel connections with two databases in MySQL.In my database.php file I have added mysql2 connection.
<?php
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_CLASS,
/*
|--------------------------------------------------------------------------
| 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', 'mysql'),
/*
|--------------------------------------------------------------------------
| 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', 'db1'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'password1'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => 'db2',
'username' => 'root',
'password' => 'password2',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'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',
],
],
/*
|--------------------------------------------------------------------------
| 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,
],
],
];
And in my routes file I added this route
Route::get('/', function()
{
$users =DB::connection('mysql2')->select('select * from users')->get();
return $users;
});
But when I browse to get the result of the query
QueryException in Connection.php line 729:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.users' doesn't exist
I need someone helps me .

//use model
$user = new \App\User();
$user->setConnection('mysql2');
$users = $user->all();
return $users;
Laravel 5.3

The default env() of database.php file direct to the .env file, so can't find table. Remove the env(), just like upper.

I changed my function on routes.php to the following and the problem solved
(get function deleted).
Route::get('/', function()
{
$users =DB::connection('mysql2')->select('select * from users');
return $users;
});

Related

Connection Error with Laravel Passport and PostgreSQL

I am installing a new API with Laravel Passport using POSTGRESQL databases. When installing Laravel and basic authentication, it works ok to the point of registering users and logging in perfectly. (take the database ok, this is important!) But when following the steps to install Passport and run the "php artisan migrate" command, Passport does not recognize that I am using POSTGRESQL and looks for the connection to MYSQL, which is non-existent. I will appreciate ideas, I don't know what else to do.
ERROR:
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'forge'#'localhost' (using password: NO) (SQL:
create table `oauth_auth_codes` (`id` varchar(100) not null, `user_id` bigint unsigned not
null, `client_id` bigint unsigned not null, `scopes` text null, `revoked` tinyint(1) not
null, `expires_at` datetime null) default character set utf8mb4 collate
'utf8mb4_unicode_ci')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the
error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
I try without result:
php artisan cache:clear
php artisan config:cache
My .ENV:
#DB_CONNECTION=mysql
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=laravel
#DB_USERNAME=root
#DB_PASSWORD=
My CONFIG/DATABASE WITH POSTGRESQL:
'default' => env('DB_CONNECTION', 'pgsql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'xxxx'),
'username' => env('DB_USERNAME', 'xxxxxxxxxxx'),
'password' => env('DB_PASSWORD', 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'someschema',
'sslmode' => 'prefer',
],
I finally found the solution in a Laravel Passport forum, it seems that in the latest versions the connection to the db is configured directly in passport (I don't know why it doesn't take the one from .env or config / databases), "publishing" (exporting) from the I sell the connection file that includes the Passport Storage Driver to the app, to be able to modify it and declare the connection exclusively for Passport. Needless to say, there are 3 different places in Laravel to declare a connection to the database, subject to chatting directly with Otwell.
Anyway, this is done with the following command:
php artisan vendor:publish --tag=passport-config
which adds a file in the config folder called passport.php, and where we find, ready to configure:
/*
|--------------------------------------------------------------------------
| Passport Storage Driver
|--------------------------------------------------------------------------
|
| This configuration options determines the storage driver that will
| be used to store Passport's data. In addition, you may set any
| custom options as needed by the particular driver you choose.
|
*/
'storage' => [
'database' => [
'connection' => env('DB_CONNECTION', 'mysql'),
],
],

Work with mysql database in Laravel 4.2

I am new to Laravel used this https://github.com/msurguy/laravel-facebook-login link to work with facebook login in Laravel. I have setup all the things but i
got following errors when establishing database connection :
Type error: Argument 1 passed to
Illuminate\Redis\Database::__construct() must be of the type array,
null given, called in
C:\wamp64\www\laravel-facebook-login-master\vendor\laravel\framework\src\Illuminate\Redis\RedisServiceProvider.php
on line 23
I want to run simple mysql database. Below is my database.php code:
return array(
'fetch' => PDO::FETCH_CLASS,
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'facebookdb',
'username' => 'root',
'password' => '',
'port' => '3603',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
),
'migrations' => 'migrations',
);
Any suggestion/help will be highly appreciated.
You are missing the definition for your redis connection in that connections array, something like this will help:
'redis' => array(
'host' => env('REDIS_HOST', 127.0.0.1),
'port' => env('REDIS_PORT', 6379),
'password' => env('REDIS_PASSWORD', null),
'database' => env('REDIS_DATABASE', 0)
)
Make sure that you add the corresponding entries in your .env file if your redis connection settings differ from the default values above.

How to setup Laravel-Session-Storage to Redis?

Hi everyone I just want to store the session's of my laravel app in redis on localhost, redis-server is running, yesterday I tried Redis::set('key') and so on and it worked perfectly but storeing session in redis leads to this error -> Redis connection [redis] not configured.
My .env-file:
BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
SESSION_DRIVER=redis
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
my config/session.php file
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Session Driver
|--------------------------------------------------------------------------
|
| This option controls the default session "driver" that will be used on
| requests. By default, we will use the lightweight native driver but
| you may specify any of the other wonderful drivers provided here.
|
| Supported: "file", "cookie", "database", "apc",
| "memcached", "redis", "array"
|
*/
'driver' => env('SESSION_DRIVER', 'redis'),
/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/
'lifetime' => 120,
'expire_on_close' => true,
/*
|--------------------------------------------------------------------------
| Session Encryption
|--------------------------------------------------------------------------
|
| This option allows you to easily specify that all of your session data
| should be encrypted before it is stored. All encryption will be run
| automatically by Laravel and you can use the Session like normal.
|
*/
'encrypt' => false,
/*
|--------------------------------------------------------------------------
| Session File Location
|--------------------------------------------------------------------------
|
| When using the native session driver, we need a location where session
| files may be stored. A default has been set for you but a different
| location may be specified. This is only needed for file sessions.
|
*/
'files' => storage_path('framework/sessions'),
/*
|--------------------------------------------------------------------------
| Session Database Connection
|--------------------------------------------------------------------------
|
| When using the "database" or "redis" session drivers, you may specify a
| connection that should be used to manage these sessions. This should
| correspond to a connection in your database configuration options.
|
*/
'connection' => 'redis',
/*
|--------------------------------------------------------------------------
| Session Database Table
|--------------------------------------------------------------------------
|
| When using the "database" session driver, you may specify the table we
| should use to manage the sessions. Of course, a sensible default is
| provided for you; however, you are free to change this as needed.
|
*/
'table' => 'sessions',
/*
|--------------------------------------------------------------------------
| Session Cache Store
|--------------------------------------------------------------------------
|
| When using the "apc" or "memcached" session drivers, you may specify a
| cache store that should be used for these sessions. This value must
| correspond with one of the application's configured cache stores.
|
*/
'store' => null,
/*
|--------------------------------------------------------------------------
| Session Sweeping Lottery
|--------------------------------------------------------------------------
|
| Some session drivers must manually sweep their storage location to get
| rid of old sessions from storage. Here are the chances that it will
| happen on a given request. By default, the odds are 2 out of 100.
|
*/
'lottery' => [2, 100],
/*
|--------------------------------------------------------------------------
| Session Cookie Name
|--------------------------------------------------------------------------
|
| Here you may change the name of the cookie used to identify a session
| instance by ID. The name specified here will get used every time a
| new session cookie is created by the framework for every driver.
|
*/
'cookie' => 'laravel_session',
/*
|--------------------------------------------------------------------------
| Session Cookie Path
|--------------------------------------------------------------------------
|
| The session cookie path determines the path for which the cookie will
| be regarded as available. Typically, this will be the root path of
| your application but you are free to change this when necessary.
|
*/
'path' => '/',
/*
|--------------------------------------------------------------------------
| Session Cookie Domain
|--------------------------------------------------------------------------
|
| Here you may change the domain of the cookie used to identify a session
| in your application. This will determine which domains the cookie is
| available to in your application. A sensible default has been set.
|
*/
'domain' => env('SESSION_DOMAIN', null),
/*
|--------------------------------------------------------------------------
| HTTPS Only Cookies
|--------------------------------------------------------------------------
|
| By setting this option to true, session cookies will only be sent back
| to the server if the browser has a HTTPS connection. This will keep
| the cookie from being sent to you if it can not be done securely.
|
*/
'secure' => env('SESSION_SECURE_COOKIE', false),
/*
|--------------------------------------------------------------------------
| HTTP Access Only
|--------------------------------------------------------------------------
|
| Setting this value to true will prevent JavaScript from accessing the
| value of the cookie and the cookie will only be accessible through
| the HTTP protocol. You are free to modify this option if needed.
|
*/
'http_only' => true,
];
My config/database.php file
<?php
return [
/*
|--------------------------------------------------------------------------
| 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', 'mysql'),
/*
|--------------------------------------------------------------------------
| 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', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'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',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
'redis' => [
'client' => 'predis',
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
],
/*
|--------------------------------------------------------------------------
| 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' => [
'client' => 'predis',
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];
If I delete/cut one of 'redis' within the database.php file it still doesn't work or i get following error:array_key_exists() expects parameter 2 to be array null given. I followed Laravel Documentation don't know what I'm doing wrong couldn't be so hard to setup 3 files with/for redis ...
Thank you very much for any help or ideas :)
config/session.php
'driver' => env('SESSION_DRIVER', 'redis'),
'connection' => 'default',
config/database.php
return [
...
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];

PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge'

I am using Laravel 5 and getting the following exception:
PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user
''#'localhost' to database 'forge'
My database configuration file is:
<?php
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_CLASS,
/*
|--------------------------------------------------------------------------
| 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' => 'mysql',
/*
|--------------------------------------------------------------------------
| 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' => storage_path().'/database.sqlite',
'prefix' => '',
],
'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,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'prefix' => '',
],
],
/*
|--------------------------------------------------------------------------
| 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' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
];
How can I solve it?
Put a .env file in your root directory and paste this code there.
APP_ENV=local
APP_DEBUG=true
APP_KEY=1CaND3OKKvOGSBAlCg6IyrRmTQWwZjOO
DB_HOST = localhost
DB_DATABASE = YOUR_DATABASE_NAME
DB_USERNAME = USER_NAME
DB_PASSWORD = PASSWORD
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
Update your database, username and password field here and it should solve your problem. In your configuration file env() function is looking for this file and variables from here.
UPDATE: You must create a blank database before running the app or migration.
We forgot to set DB_USERNAME= in the .env file, so we got this error:
SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to
database 'forge'
Open the .env file and edit it. Just set up correct DB credentials:
DB_USERNAME= //Your Database Username
DB_USERNAME should be set to root if you do not have a default username in the installation time
After changes of .env enter this command in your terminal for clear cache:php artisan config:cache
NOTE: If there is still error
If you are using the PHP's default web server (eg. php artisan serve) you need to restart your server
OR
If you have used XAMPP then restart your Apache server
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=
The DB_USERNAME= should be set to root if you do not have a default username in the installation of MySQL in XAMPP.
Sometime in the future. This will be a basic setup if using MySQL instead of Homestead. Config->database.php file
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'yourDatabaseName'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and in your .env file
APP_ENV=local
APP_DEBUG=true
APP_KEY=ruA9CAKRJCFgLOD1nc5o1BmvaTGokasi
DB_HOST=localhost
DB_DATABASE=yourDatabaseName
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
and if you run the command it should work smoothly.
Had the same problem after changing the ownership of the account, so I ended up doing the following:
(This worked for me)
Take note of your actual user and password
Go to your cPanel (that is if you're using cPanel)
From Databases tab, select MySQL Databases.
Navigate to the Current Users
Delete the user that corresponds to the database You are having conflict with.
After deletion, go to the Add User to Database Tab and reenter the username and password, this will regenerate the connection with the current owner privileges.
You can check the current environment with: php artisan env
next
create under config/development | staging (folder)
save a file database.php with development | staging access
edit .env file APP_ENV=development | staging | production
Try to go to database.php file correct your database name,username and password
<?php
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', 'mysql'),
/*
|--------------------------------------------------------------------------
| 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', 'msm'),----database name
'username' => env('DB_USERNAME', 'root'),--localhost username
'password' => env('DB_PASSWORD', ''),--localhost password
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'msm'),----database name
'username' => env('DB_USERNAME', 'root'),--localhost username
'password' => env('DB_PASSWORD', ''),--localhost 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,
],
],
];
This problem can be caused by the file not having the expected ".env" extension.
If you have file extensions hidden in Windows (which is on by default), then it can easily look like the extension is ".env", when it is actually something like, like ".env.txt".
This means that the program won't be able to find your ".env" file because it doesn't have the right extension.
Open the file with an editor like Sublime Text, and you'll see what the actual extension is. If it has a ".txt" extension, delete that part and re-save.
I was getting the same message with empty username. The username was always blank.
The issue was with MySQL. The SQLSTATE error message is being passed directly from MySQL.
So then I deleted the database and user. Re-created the database in the Databases tab of PhpMyAdmin. And importantly separately added the user and privileges. This fixed the issue.
I could connect from the MySQL client and run migrations.
This issue also occur when you use multiple databases in same project/query
I had the same issue solved by using same DB_USERNAME for all databases ,
because i was use multiple databases in same project and every database DB_USERNAME was different so when i run the query select,update, delete etc from multiple database then was get this error.
because if you use more then one table in query and the database DB_USERNAME is different you will get this error.
Solution:
If you using/connect more then one databases then you should use same DB_USERNAME for all databases
If you don't require login while accessing the database
Try database username as root and password field leave blank
This solves my problem

FatalErrorException in laravel5

I am using laravel 5, I want to get data from database but it throwing the below error:
FatalErrorException in LearnController.php line 38: Class 'App\Http\Controllers\DB' not found
I have been added the database details then also i am able get the title from ads_new table
Here's my code:
<?php
namespace App\Http\Controllers;
class LearnController extends Controller {
/*
|--------------------------------------------------------------------------
| Home Controller
|--------------------------------------------------------------------------
|
| This controller renders your application's "dashboard" for users that
| are authenticated. Of course, you are free to change or remove the
| controller as you wish. It is just here to get your app started!
|
*/
/**
* Create a new controller instance.
*
* #return void
*/
/**
* Show the application dashboard to the user.
*
* #return Response
*/
public function home()
{
$users = DB::table('ads_new')->get();
foreach ($users as $user)
{
var_dump($user->title);
}
}
}
database.php
<?php
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_CLASS,
/*
|--------------------------------------------------------------------------
| 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' => 'mysql',
/*
|--------------------------------------------------------------------------
| 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' => storage_path().'/database.sqlite',
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'autoclick'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'autoclick'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'autoclick'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'prefix' => '',
],
],
/*
|--------------------------------------------------------------------------
| 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' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
];
The DB façade is in the global namespace, so you need to import it at the top of your controller:
<?php namespace App\Http\Controllers;
use DB;
Have a read up on namespaces in PHP: http://php.net/manual/en/language.namespaces.php
Try this solution:
From Matt Burrow comment
Add a \ before DB like so; \DB or add use \DB; at the top.
So it should be like :
class LearnController extends Controller {
public function home()
{
$users = \DB::table('ads_new')->get();
foreach ($users as $user)
{
var_dump($user->title);
}
}
}

Resources