I checked my config/database.php
I have:
'default' => env('DB_CONNECTION', 'mysql'),
and in my connection I have:
'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,
but it seems that my DB configuration areh happening somewhere elese, I also defined my varioables in my .env file. But when I run the migrate in my app directory, it errors out :
[Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name
'usernam e' (SQL: alter table users add username varchar(191)
null)
although I have already taken the migrate file for users out (renamed the php extension of it)
Any help will be appreciated.
edit: does thi happen that migration files are stored somewhere other than /database/migrations ?
The error duplicate column name 'username' indicates the database has a table called users which has the column username in.
What seemed to happen: you took out the migration file out of Laravel before you did php artisan migrate:rollback. This left the database with all the schema structure that the migration wrote out. So obviously you can't run the same migration again because the same structure is already found in the database.
What you should do: log into your mysql server, drop, and create the database again.
mysql -u root -p then it will prompt you for the password of your mysql server.
drop database your-db-name
create database your-db-name
Then run the migrations again, php artisan migrate.
Further diagnose method
You can create a file 'database.sqlite' under the /database/ folder and change your .env's DB_CONNECTION to sqlite.
Run migrations and rollback and run it again. If it messes up at any point, it means the migration files have got a problem in them.
Related
I am getting this error while trying to save an object into DB.
SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: insert into cards (card_price, active, updated_at, created_at) values (0, 1, 2019-10-10 15:14:43, 2019-10-10 15:14:43))
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cardgame
DB_USERNAME=root
DB_PASSWORD=P#assword1!
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_T_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'),
]) : [],
],
CardController.php
public function generateCards()
{
$card = new Card();
$card->card_price = 0;
$card->active = 1;
$card->save();
}
Web.php
Route::get('/generate-cards', 'CardController#generateCards');
Card.php
class Card extends Model
{
protected $guarded =[];
}
Migration file
public function up()
{
Schema::create('cards', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('card_price');
$table->integer('active');
$table->timestamps();
});
}
I've tried clearing cache and also have edited DB_PASSWORD To DB_T_PASSWORD as this corrected a similar issue earlier. Double checked the DB name, passwords etc & also other projects are already running also. I'm not able to figure it out.
Go to your localhost server and drop the database and recreate a new one named cardgame
Go to your laravel project in console and run php artisan config:cache command. That way all your env variables will be used.
Run php artisan:migrate to run your migrations for your database and create your tables.
If you do the above 3 steps in that order you should be fine.
You must clear the cache because your old configuration is in the cache file, just run this command in your terminal for clear cache:
php artisan config:cache
You can solve this problem by 2 simple step.
Step:1 php artisan config:cache
step:2 php artisan migrate
I had the same problem and I solved it following these steps:
1) Stop the server
2) Run php artisan config:clear .This command line will remove
bootstrap\cache\config.php file
3) Start the server and It should work fine now
create database in php admin then migrate your database again
php artisan migrate , it's work 100%
I was also experiencing the same error and implimented all the solutions mentioned above but the error kept reoccuring. But I edited the.env and env.example file where the database = "laravel" to the name of my project for instance transportsytem and it worked.The best solution to the problem is editing both .env and env.example files with the name of your database that you created .
I'm trying to connect Laravel with a multiple sqlserver databases, I defined the connections on my database.php file correctly :
'connections' => [
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '16000'),
'database' => env('DB_DATABASE', 'dbname'),
'username' => env('DB_USERNAME', 'me'),
'password' => env('DB_PASSWORD', 'me'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
'sqlsrv2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '16000'),
'database' => env('DB_DATABASE_2', 'dbname2'),
'username' => env('DB_USERNAME', 'me'),
'password' => env('DB_PASSWORD', 'me'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
and set my .env file
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=16000
DB_DATABASE=dbname
DB_USERNAME=me
DB_PASSWORD=me
I try to verify the connectivity to my sql server database from another application and it's working perfectly. then I created a model on my laravel application and once I type php artisan migrate I get this error :
anyone can help to resolve this problem ?
UPDATE !
After adding pdo sqlsrv extensions to my ext folder and my php.ini i get this error :
[![enter image description here][2]][2]
[![enter image description here][3]][3]
UPDATE 3 :
After installing odbc 13 i got this problem
Ah, WAMPServer has 2 seperate php.ini files.
One is used by PHP under Apache and the other by the PHP CLI.
I see you are using the CLI, so you need to check that you have included the SQL Server drivers extension in the CLI php.ini file.
You will find that in:
C:\wamp64\bin\php\php{version}\php.ini
You have to edit this manually, there is no menu link to do this, the menu link to edit the php.ini only edits the PHP under Apache version of php.ini
Make sure you edit the php.ini file in the folder that matches the version of PHP you are actually using for this project, as of course you can have multiple versions of PHP installed under WAMPServer
UPDATE: Second Problem
You have added the 32bit AND 64 bit SQL Server drivers to your ext folder!
Your WAMPServer menu shows
1. php_sqlsrv_72_ts_x64
2. php_sqlsrv_72_ts_x32
3. php_pdo_sqlsrv_72_ts_x64
4. php__pdo_sqlsrv_72_ts_x32
as you are using 64 bit wamp, you should only have these 2
1. php_sqlsrv_72_ts_x64
2. php_pdo_sqlsrv_72_ts_x64
Ive just built my new app in Laravel 5.1 and im trying to push it live. When i try to composer install i am getting this error.
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO)
Script php artisan clear-compiled handling the post-install-cmd event returned with an error
[RuntimeException]
Error Output:
I am using a environment file on both local and production so i have no idea whats going on. Any ideas?
Its like my environment file is not getting picked up on forge.....
config/datasbase.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' => 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' => 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', ''),
'charset' => 'utf8',
'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 reason this is happening is because forge is running the post install command on your project and your are probably accessing the database in one of your service providers to for may be a a variable you want to attach to your views, try commenting the code in the service provider, install the repository and then push the code up again it should work fine
The reason this was happening is because i was using the 'Install Repo' functionality that forge provides to install my full project. Now because there was no environment set on the server it was failing upon install.
To get around it, i had to hardcode the production database credentials, install the repo then create the env file. Then once this was done remove the hardcoded credentials (not great i know).
It seems setting the .env before the install doesn't work either. So you wouldn't run into this sort of issue if you had your repo installed on a server from the beginning of your build.
But my project is now live and running correctly. Thanks for the replies.
try this
1
in app/database.php
Replace localhost with 127.0.0.1
'host'=> env('DB_HOST', 'localhost') -->'host' => env('DB_HOST', '127.0.0.1')
Also, in .env
DB_HOST=localhost --> DB_HOST=127.0.0.1
2
Try specify environment
php artisan migrate --env=local
3
Check to see if the MySQL is running by run
mysqladmin -u homestead -p status Enter password: secret
I got
Uptime: 21281 Threads: 3 Questions: 274 Slow queries: 0 Opens: 327 Flush tables: 1 Open tables: 80 Queries per second avg: 0.012
Which mean it's running.
4
Check MySQL UNIX Socket
5
make sure that your .env file looks something like this:
APP_ENV=local
APP_DEBUG=true
APP_KEY=YOURAPPKEY
DB_HOST=localhost
DB_DATABASE=YOURDBNAME
DB_USERNAME=YOURPHPMYADMINUSERNAME
DB_PASSWORD=YOURPHPMYADMINPASS
6
try changing localhost into 127.0.0.1 in .env DB_HOST or try add your mysql port
Make sure MySQL user has all accessing privileges for given ip while connecting the MySQL. For further reference please go through the below link.
http://tech2smooth.blogspot.in/2014/09/create-mysql-user-and-grant-permission.html
It's hard to say anything for sure without seeing the .env file, but it looks like it's picking up the username from somewhere - the default is 'forge' and yet the error message has the username as an empty string - ''#localhost.
Check your env file, make sure there are no spaces after the = and that the variable names match the ones in the database config. I've also had issues where I needed to wrap passwords and other values that had special characters in ''. Finally, remember that case matters - I've seen problems where everything worked locally and failed on the dev server because someone had changed the case of a variable. My mac didn't care, but the ubuntu server did.
I am using schema builder to create a Mysql table. I am using Ubuntu 14.04.
My connection for Mysql in config/database.php is :
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'firstapp'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And Code for creating table with schema builder on routes.php
Route::get('/', function () {
Schema::create('art', function($newtable)
{
$newtable->increments('id');
$newtable->string('artists');
$newtable->string('title',500);
$newtable->text('discription');
$newtable->date('created');
$newtable->date('exhibition_date');
$newtable->timestamps();
});
return view('welcome');
});
Thanks
As far a as i know , you must create a migration file using
php artisan migrate:make yourtable
Then yo must go to database folder you will find a new migration file created , then you must open it , and fill it with your schema , once you finish , then run migration
php artisan migrate
And that's all you will see your new table created.
Migrations laravel 5
First i was using xampp mysql and this was also started /etc/init.d/mysql from terminal, Since i am using Ubuntu 14.04.
I solved the problem which i posted above then another problem came up Saying SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES).
I changed the .env file which is placed in the root directory of laravel and i changed the Database Name name and password which i was using for mysql.
It solved that problem too and i am up and running.
My website works, its clearly accessing the database. I can log in and i can create records, etc...
Ive created a new migration, and I'm trying to insert it, but when i run
php artisan migrate
i get the error
[PDOException]
SQLSTATE[HY000] [2002] Connection Refused
My database config has the passwords hidden in the environment so my config looks like this
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOSTNAME'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
my environment variables are loading correctly. What am i missing?
I figured it out. Apparently the environment that artisan was seeing was different from the environment i wanted it to see, so it was using the wrong host
php artisan migrate --env=production