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.
Related
I installed Openclassify latest version. In the installation, first I got errors from php artisan install, I changed database.php from:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => env('DB_PREFIX', ''),
'strict' => false,
'engine' => env('DB_ENGINE'),
],
into:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'openclassify'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => env('DB_PREFIX', ''),
'strict' => false,
'engine' => env('DB_ENGINE'),
],
Then I ran composer update and I give php artisan install --ready command, I got this error in the command line:
In
2015_02_27_101227_anomaly.module.preferences__create_preferences_fields.php
line 12: Cannot declare class
AnomalyModulePreferencesCreatePreferencesFields, because the name is
already in use
I ran php artisan serve checked in the browser and I got this error in the browser.
ErrorException (E_ERROR) Unknown "setting_value" function.
This is the error I got in browser http://127.0.0.1:8001/installer when I ran php artisan serve command
The Lines show the errors are as follows:
C:\wamp64\www\openclassify\vendor/visiosoft/defaultadmin-theme/resources/views/partials/metadata.twig
<style type="text/css">
{{ asset_inline("theme.css") }}
{{ asset_inline("build.css") }}
{% if setting_value('visiosoft.theme.defaultadmin::dark_mode') %}
/*Dark mode is active*/
{{ asset_inline("visiosoft.theme.defaultadmin::css/dark_mode.css") }}
{% endif %}
</style>
Kindly look into this issue and share your opinion regarding this.
Hello I need to change DB_HOST from localhost to another IP or hostname on Laravel.
I have change my .env to:
DB_CONNECTION=mysql
DB_HOST=192.168.1.55
DB_SOCKET=/var/run/mysqld/mysqld.sock
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=userdb
DB_PASSWORD=12345
I also change config/database.php to:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '192.168.1.55'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'testdb'),
'username' => env('DB_USERNAME', 'userdb'),
'password' => env('DB_PASSWORD', '12345'),
'unix_socket' => env('DB_SOCKET', '/var/run/mysqld/mysqld.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'),
]) : [],
],
I have execute command:
php artisan config:clear
php artisan optimize:clear
I try to migrate but the app still apointing to localhost, not to host I already set to IP, here is the error:
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'userdb'#'localhost' (using password: YES)...
It's only solved by using DATABASE_URL not standard ENV, I don't know why?:
DATABASE_URL=mysql://userdb:12345#dbserver:3306/testdb
and still being a mystery.
I build a multi-tenant app in laravel, That works properly in local but it does not work on production/staging. Whenever I try to login, it shows an error that : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tenancy.users' doesn't exist.
I have already run migrations and also try php artisan config:ca.
Package used : composer require "hyn/multi-tenant:5.3.*"
.env :
LIMIT_UUID_LENGTH_32=true
APP_URL_BASE=testdomain
APP_URL=http://${APP_URL_BASE}
DB_CONNECTION=system
TENANCY_HOST=127.0.0.1
TENANCY_PORT=3306
TENANCY_DATABASE=tenancy
TENANCY_USERNAME=root
TENANCY_PASSWORD=
database.php :
'default' => env('DB_CONNECTION', 'tenant'),
....
'system' => [
'driver' => 'mysql',
'host' => env('TENANCY_HOST', '127.0.0.1'),
'port' => env('TENANCY_PORT', '3306'),
'database' => env('TENANCY_DATABASE', 'tenancy'),
'username' => env('TENANCY_USERNAME', 'tenancy'),
'password' => env('TENANCY_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'tenant' => [
'driver' => 'mysql',
'host' => env('TENANCY_HOST', '127.0.0.1'),
'port' => env('TENANCY_PORT', '3306'),
'database' => env('TENANCY_DATABASE', 'tenancy'),
'username' => env('TENANCY_USERNAME', 'tenancy'),
'password' => env('TENANCY_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
.....
I also built a laraval mts, with laravel 8. It works locally and i have run into a similar issue.
ensure your .htaccess file is correct. Mine runs in cpanel and i found it was being overwritten everytime i updated the repo with master
make sure you have the databases set up. I use phpmyadmin on cpanel so i had to build the databases.
create a database user and give them access rights to the databases.
update .env to use your newly created database user
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.
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.