Having trouble with using multiple database in laravel 5.7? - laravel

I'm having a problem with Laravel 5.7, it don't recognize a second database connection. Database 2 has a 'countries' table that Database 1 doesn't. But laravel insist with connect the Database 1. This is the error and actual code, please help! Thanks in advance
ERROR
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'database_1.countries' doesn't exist (SQL: select * from countries)
MODEL
class Country extends Model {
use SoftDeletes;
protected $connection = 'mysql_2';
protected $table = 'countries';
protected $fillable = ['...'];
}
CONTROLLER / DEBUGGINB
class CountriesController extends Controller {
public function index(){
// neither works, this
$countries = DB::connection("mysql_2")->select('select * from countries');
// or this
$countries = Country::all();
dd($countries); --> both give ERROR
}
DATABASE.PHP
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_1'), //forge
'username' => env('DB_USERNAME', 'root'), // forge
'password' => env('DB_PASSWORD', ''),
.......
],
'mysql_2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_2'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
.......
],
.ENV file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_1
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=mysql_2
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_2
DB_USERNAME=root
DB_PASSWORD=
and CACHE is updated:
php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!

In your database.php file try to change
'mysql_2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE2', 'database_2'), // THIS IS THE ONE THATS CHANGED
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
.......
],
.ENV FILE
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_1
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=mysql_2
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE2=database_2 // We change 'DB_Database' to 'DB_Database2'
DB_USERNAME=root
DB_PASSWORD=

Another process you can try -
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_1
DB_USERNAME=root
DB_PASSWORD=
CUSTOM_DATABASE=database_2
CUSTOM_USERNAME=root
CUSTOM_PASSWORD=
database.php
'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', ''),
...................................
],
'custom' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('CUSTOM_DATABASE', 'forge'),
'username' => env('CUSTOM_USERNAME', 'forge'),
'password' => env('CUSTOM_PASSWORD', ''),
..........................................
],
YourModel.php
protected $connection = 'custom';
protected $fillable = [......];
If you use protected $connection in your model then you can use eloquent as usual. If so, then you can run your query like this -
$countries = Country::all();
And in your migration file -
public function up()
{
Schema::connection('custom')->create('your-table-name', function (Blueprint $table){
$table->bigIncrements('id');
...........................
$table->timestamps();
});
}

Related

SQLSTATE[HY000] [2002] No such file or directory (SQL: select count(*) as aggregate from `admins` where `email` = hello#gmail.com) error on laravel8

This is my web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/register',[App\Http\Controllers\AdminController::class, 'ShowRegister']);
Route::post('/register',[App\Http\Controllers\AdminController::class, 'Register'])->name('register');
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=
database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'project'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
'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'),
]) : [],
],
when i try to submit form of register it gives me errors as
Illuminate\Database\QueryException SQLSTATE[HY000] [2002] No such file
or directory (SQL: select count(*) as aggregate from admins where
email = hello#gmail.com) http://127.0.0.1:8004/register
I searched about it too and came to solution that adding unix_socket fixed issue but unfortunately it doesnot help me either !!
what can be issue here ? Help me please !!

SQLSTATE[08006] [7] FATAL: database "test-database" does not exist when i try to migrate in laravel

so im trying to use php artisan migrate but i get this error :
SQLSTATE[08006] [7] FATAL: database "test-database" does not exist (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')
the problem is there is no database "test-database"
here is my env:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=Batman
DB_USERNAME=postgres
DB_PASSWORD=123456
and here is my database.conf
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'Batman'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
i really dont get it. please help
try pgAdmin, it has a deb package, it should work fine on Ubuntu.

SQLSTATE[HY000] [2002] Connection refused (SQL: insert into users(name,email) values(mahmud,mahmudul#gamil.com))

I am using MySql Workbench for database. All doing well when I migrate my project.But when I want to insert data to the users table that time occurs this error "SQLSTATE[HY000] [2002] Connection refused (SQL: insert into users(name,email) values(mahmud,mahmudul#gamil.com))"
I am using windows, laravel 5.8,Homestead, MySql Workbench.
here some file:
.env:
enter image description here
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:KRf7nQxvFaheTWWPsflVa4KYZXR5Jwo5IvwXxogDJn4=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
database.php:
enter image description here
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '33060'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'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'),
]) : [],
]
web.php:
enter image description here
Route::get('/', function () {
return view('welcome');
});
Route::get('/insert',function(){
DB::insert("insert into users(name,email) values(?,?)",["mahmud","mahmudul#gamil.com"]);
});
page like:
enter image description here
please help me how to solve it.
thanks advance.
change .env and database.php:
.env:
replace
DB_HOST = 127.0.0.1
to
DB_HOST = localhost
and
database.php:
replace
'host' => env('DB_HOST', '127.0.0.1')
to
'host' => env('DB_HOST', 'localhost')
note: MySql Connection Hostname will be 127.0.0.1 and port will be 33060:
go to your config/database.php and paste below code
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],

Laravel 5.8 Installation Error In database.php line 58: Undefined class constant 'MYSQL_ATTR_SSL_CA'

When I tried to install Laravel 5.8 it throws Error
In database.php line 58:
Undefined class constant 'MYSQL_ATTR_SSL_CA'
After this I have tried to run the application on server. It works fine sometimes. Sometimes it throws the same error. I couldn't run any commands on Artisan too. Why this happens and How to solve this?
New Laravel releases have this error. Look in config/database.php, you will see something like :
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]),
Secured Applications have an environment file that contains data for specific machine and software configurations like Database name and password, Email and password, value to tell if it's for development or production, etc.
Laravel loads them in constant accessible via global function env().
There is a file .env that contains those special values in Laravel. So open it and at the bottom of Database section, add your certificate path value:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog-db
DB_USERNAME=root
DB_PASSWORD=fakepass
MYSQL_ATTR_SSL_CA=relative/or/absolute/path/to/certificate.extension
Or if you're not planning to use SSL certificate for MySQL connection like most of us, then just comment it in config/database.php :
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
//'options' => array_filter([
// PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
//]),
Like it is currently at Laravel/Laravel master : https://github.com/laravel/laravel/blob/master/config/database.php
If you don't have MySQL extension, you should use:
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
in config/database.php instead of
'options' => array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]),
It's now included by default in Laravel 5.8 https://raw.githubusercontent.com/laravel/laravel/master/config/database.php (though it's probably not released yet).
So just update code with this above and you are good to go.

YII framework access environmental variables

In Laravel project we can create a file .env and store username and password as
.env file
DB_USERNAME=homestead
DB_PASSWORD=secret
And in database config/database.php
I can use it as
'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,
],
How to do the same in YII framework
Laravel makes use of the PHP dotenv package - you can add it to Yii with Composer:
https://github.com/vlucas/phpdotenv
Note that env() is specific to Laravel, you'll need to use getenv() in Yii.

Resources