I have installed a fresh copy of Laravel.
I need it so i can set the environment to the URL.
So for instance in L4 i used this within the start.php file
$env = $app->detectEnvironment(function()
{
return isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
});
This then loaded the correct environment for the project, so if we had a local domain dev.laravel.com it would load in the environment variables from dev.laravel.com
I cannot do this within Laravel 5.
Any guesses how I can apply this?
You can for example do it for use different database, then you have some different variables on .env like:
DB_HOST=localhost
DB_DATABASE=database1
DB_USERNAME=foo
DB_PASSWORD=foo
DB_HOST_2=localhost
DB_DATABASE_2=database2
DB_USERNAME_2=foo
DB_PASSWORD_2=foo
and in config/dababase.php:
'connections' => [
'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,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', 'localhost'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and finally use:
Config::set('database.default', "mysql") or Config::set('database.default', "mysql2")
depending you need.
For example I use to change database connection after login user doing it at controller abstract class changing the value depending session var, that is changed after login depending type of user:
abstract class Controller extends BaseController {
use DispatchesCommands, ValidatesRequests;
public function __construct()
{
Config::set('database.default', Session::get('myapp.database','mysql'));
}
}
After login I put to change this value:
Session::set('myapp.database', 'mysql2') or Session::set('myapp.database', 'mysql')
and in the next call to controller the database connections is changed.
You need to put a .env file in each of your environments - and put the specific config for those environments into the .env file
So one one environment you might put
APP_ENV=local
APP_DEBUG=true
DB_PASSWORD=secret
and in another .env on a different environment you might put
APP_ENV=staging
APP_DEBUG=false
DB_PASSWORD=other
Related
I created a database using WAMP. Now I have to move everything to Laravel.
How can I use this database?
I have a lot of stored procedures written there, I would really like to use them again.
I tried changing .env file and config/database.php file, but it won't help.
config/database.php
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'db_file'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'psi'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'latin2_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_file
DB_USERNAME=root
DB_PASSWORD=psi
You don`t need to configure both env and database.php.Configure one.I recommend you to configure env file .You need a model to access the database.
Models-
https://laravel.com/docs/5.4/eloquent
Controllers-
https://laravel.com/docs/5.4/controllers#resource-controllers
Always follow the documentation
I'm using laravel5 with Homestead (homestead.app).
At beginning i use localhost, that time laravel Auth working fine.
But move to homestead, its not working(cant connect with DB).
Any configurations need to change ?
MYSQL : Connection
DB_HOST=localhost
DB_DATABASE=laravel5
DB_USERNAME=homestead
DB_PASSWORD=secret
First ensure you really have .env file uploaded to the host.
The .env file is used by config/database.php file.
Ensure that it contains a line
'default' => env('DB_CONNECTION', 'mysql'),
and these lines as well:
'connections' => [
//...
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
//...
],
I'm learning laravel. I newly created a project and trying to create model, i set up the db config database.php and type php artisan migrate, but there is a error message
C:\xampp\htdocs\l1\blog>php artisan migrate
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (usin
g password: YES)
I see it is using .env config but not using config in my config.php, why?
You can change the .env file to suit your database config settings as follows
DB_HOST=localhost
DB_MAIN=MYDB
DB_USERNAME=root
DB_PASSWORD=root
Change the values of those keys according to your database connection. And place them in .env file.
restart your server once and proceed.
Other wise you can directly place those values in database.php file by replacing something like env('DB_USERNAME'),env('DB_PASSWORD') with direct values in either single or double quotes as follows
'main' => [
'driver' => 'mysql',
'host' => '',
'database' => 'mydb',
'username' => 'root',
'password' => 'root123',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
The code actually present in database.php file can be something as follows
'main' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_MAIN', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
You can simply replace these values with above mentioned values.
I have API written in lumen(laravel). I am using Eloquent for my models.
What I need to do is to use different databases based on url (endpoint).
For example I have http://apiprovider.com/api/v1/ as base API url and it connects to the api_v1 database, but I need to use another database if v2 is used http://apiprovider.com/api/v2 for instance api_v2 database.
All classes and laravel application should be the same, only different database according to version.
Database settings is stored in .env file.
Please suggest the right way to implement this ? Or at least possible ways.
Thanks.
It's just an idea, i haven't tried it but a simple way would be switching between the two databases from a middleware.
For example you could define the two connections available in database.php :
'connections' => [
'mysql1' => [
'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,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', 'localhost'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
Then create a middleware and switch the DB in the handle method
public function handle($request, Closure $next)
{
//check the request URL and decide what DB to use
//set the DB for this request
Config::set('database.default', $dbname );
}
I think that Config::set('database.default', $dbname); will work only for the current request, but it would do what you need
I'm developing Laravel app on my local server where I have database and also I'm putting in online in production environment.
Each environment has different db connection information. So far I dealt with this just commenting information when I'm committing like this:
'mysql' => [
'driver' => 'mysql',
'host' => $host,
'database' => $database,
'username' => $username,
'password' => $password,
// 'host' => env('DB_HOST', 'localhost'),
// 'database' => env('DB_DATABASE', 'forge'),
// 'username' => env('DB_USERNAME', 'forge'),
// 'password' => env('DB_PASSWORD', ''),
// 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
but this is sure wrong approach. How can I set different database connection information for each environment is some better way?
Use the .env file instead.. Have a .env in your production and one in your development..
Take a look at your .env.example
Laravel Environment Configuration Documentation