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
Related
I'm trying to add authentication to my Laravel project. My database configuration is:
'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' => null,
],
And the .env file contains:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=example
DB_USERNAME=root
DB_PASSWORD=
When I try to register, this error happens:
I'm using XAMPP and my phpMyAdmin table structure is shown in this picture:
Its because remember_token field is not allowing to store null.
so either you need to provide value for remember_token or allow remember_token to have null values.
I am new to Laravel5. I tried to create migration table but I get this error
Error Log
This is the database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'larashop'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'melody'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
and this is .env
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=larashop
DB_USERNAME=root
DB_PASSWORD=melody
Please help.
Your screenshot shows "localhost" and your code shows "127.0.0.1". MySQL treats "localhost" and "127.0.0.1" differently.
Make sure you have granted appropriate permissions for 'root'#'localhost' in your database.
http://dev.mysql.com/doc/refman/5.7/en/grant.html
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 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