I am having a problem while running php artisan migrate on my second table after running my first users table in laravel. When I run the second table migration, I get this error:
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
php artisan migrate run all the migrations inside database/migrations folder. So, first you migrated the users table and then you created a new second table migration and ran this command so laravel tried to migrate the users migration again which already exists inside your database and this error is being thrown.
You need to run:
php artisan migrate:refresh
It'll will roll back all of your migrations and then execute the migrate command.
If you are starting off using Laravel Spark or you have used an authentication layer, then the "users" table will give you that error because it already exists. Look through your migrations and you should find a table named as such.
The other possibility is that you have not set up your environment variables correctly and you are using the same database as another project. If that previous project has a "users" table, then this error will also be shown.
Run this php artisan migrate:reset.
Next time when you want to add another table,you need to check first whether the table already exists or not. You can do this by using
if(!Schema::hasTable('users')){
// Write your schema create code here
}
Save the file and than run php artisan migrate command.
I have solved this Problem
Laravel 5.5 Error Base table or view already exists: 1050 Table 'users' already exists
by Changing My create_users_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::dropIfExists('users');
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
I want to use Laravel's migration to manage my database. But I need to have a single configuration file where I store the schema. Something like this :
{
user: {
"id":"increments",
"name":"string",
"timestamps":"timestamps()"
}
}
And when I change this file again to the text below
{
user: {
"id":"increments",
"name":"string",
"password":"string",
"timestamps":"timestamps()"
}
}
I want to be able to run a command and have the database be changed without losing any data or creating an additional config file.
Can I achieve this using laravel migrations or if you know of any other solution that can help me and I would be able to use that on laravel without losing any of laravel's database management tools, please comment.
Thank you.
php artisan make:migration add_password_to_user ( are you sure the table is called user and not users? )
In your new migration
public function up()
{
Schema::table('user', function($table) {
$table->string('password');
});
}
Don't forget the rollback
public function down()
{
Schema::table('user', function($table) {
$table->dropColumn('password');
});
}
The table should be users if you have the model User .. otherwise make sure to have protected $table = 'user'; on the model.
https://laravel.com/docs/5.3/migrations
The --table and --create options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options simply pre-fill the generated migration stub file with the specified table:
php artisan make:migration create_users_table --create=users
php artisan make:migration add_votes_to_users_table --table=users
I have an existing database table and I want to add column on it. However, as I run the php artisan migrate command, it says nothing to migrate. But I already add a Schema for adding table columns. I have read some articles and links that I should run the php artisan migrate:refresh first before the new columns to be added.The problem is, it will erase my existing data in my table. Is there any way I could perform the migration and successfully add columns in my table without deleting my data? Please help me with this. Thanks a lot. Here is my migration code.
public function up()
{
//
Schema::create('purchase_orders', function(Blueprint $table){
$table->increments('id');
$table->string('po_code');
$table->text('purchase_orders');
$table->float('freight_charge');
$table->float('overall_total');
$table->timestamps();
});
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
Schema::drop('purchase_orders');
}
I want to add column shipped_via and terms in my purchase_orders table.
Use below command to modify the existing table
php artisan make:migration add_shipped_via_and_terms_colums_to_purchase_orders_table --table=purchase_orders
use --create for creating the new table and --table for modifying the existing table.
Now a new migration file will be created. Inside the up() function in this file add these line
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
And then run php artisan migrate
Laravel has a table in your database where it keeps track of all the migrations that are already executed. So by only changing the migration file Laravel will not automatically rerun that migration for you. Cause the migration is already executed by Laravel.
So the best thing to do is to just create a new migration and put the piece of code in it you already have (you were on the right track!).
public function up()
{
//
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
You don't need to populate the down function case the table will be dropped by your current purchase_orders migration.
To migrate the new migration just run:
php artisan migrate
I am using composer for migrating two tables like this:
php artisan make:migration create_two_tables --create="projects","tasks"
Its creating the file in database->migration folder.
but while migrating using
php artisan migrate
its creating only table in database like this:
'projects,tasks'
as one table.
I want only one file and only one command like what I do in the top for migrating two tables in db.
Is there any possibility to get this?
Note: My superior ordered me to not change the database migration file at any cost...
Can anyone help me out here...
The make:migration command is just to create a new migration file from a template. To actually define the migration you normally have to edit that file. So in your case you would do this:
php artisan make:migration create_two_tables --create="projects"
Then open the ***_create_two_tables.php migration file and add the second table:
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('projects', function(Blueprint $table)
{
});
Schema::create('tasks', function(Blueprint $table)
{
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('projects');
Schema::drop('tasks');
}
Usually you also want to add actual columns to your tables. And that you do inside the Schema::create closure:
Schema::create('projects', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
// and so on
});
Read more about creating tables with the Schema Builder here
If you want to just create two tables through artisan and not add any columns to those tables you can create two migrations. Create one for each table. When you run
php artisan make:migration create_projects_table --create=projects
php artisan make:migration create_tasks_table --create=tasks
php artisan migrate
it will execute both migrations and create both tables for you.
I can't figure out how to add a new column to my existing database table using the Laravel framework.
I tried to edit the migration file using...
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
In terminal, I execute php artisan migrate:install and migrate.
How do I add new columns?
To create a migration, you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models
for Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
for Laravel 3:
php artisan migrate:make add_paid_to_users
You then need to use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
and don't forget to add the rollback option:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('paid');
});
}
Then you can run your migrations:
php artisan migrate
This is all well covered in the documentation for both Laravel 4 / Laravel 5:
Schema Builder
Migrations
And for Laravel 3:
Schema Builder
Migrations
Edit:
use $table->integer('paid')->after('whichever_column'); to add this field after specific column.
I'll add on to mike3875's answer for future readers using Laravel 5.1 and onward.
To make things quicker, you can use the flag "--table" like this:
php artisan make:migration add_paid_to_users --table="users"
This will add the up and down method content automatically:
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
Similarily, you can use the --create["table_name"] option when creating new migrations which will add more boilerplate to your migrations. Small point, but helpful when doing loads of them!
In case you want to add new column as a FOREIGN KEY to an existing table.
Create a new migration by executing this command : make:migration
Example :
php artisan make:migration add_store_id_to_users_table --table=users
In database/migrations folder you have new migration file, something like :
2018_08_08_093431_add_store_id_to_users_table.php (see the comments)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddStoreIdToUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
// You probably want to make the new column nullable
$table->integer('store_id')->unsigned()->nullable()->after('password');
// 2. Create foreign key constraints
$table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
// 1. Drop foreign key constraints
$table->dropForeign(['store_id']);
// 2. Drop the column
$table->dropColumn('store_id');
});
}
}
After that run the command :
php artisan migrate
In case you want to undo the last migration for any reason, run this command :
php artisan migrate:rollback
You can find more information about migrations in the docs
If you're using Laravel 5, the command would be;
php artisan make:migration add_paid_to_users
All of the commands for making things (controllers, models, migrations etc) have been moved under the make: command.
php artisan migrate is still the same though.
In Laravel 8
php artisan make:migration add_columnname_to_tablename_table --table=tablename
then after creating migration in
public function up()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
$table->datatype('column_name')->nullable();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
$table->dropColumn('column_name');
});
}
then run
php artisan migrate
if you face error then rename the migration name with the date before the table created and then run again php artisan migrate
You can add new columns within the initial Schema::create method like this:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
If you have already created a table you can add additional columns to that table by creating a new migration and using the Schema::table method:
Schema::table('users', function($table) {
$table->string("title");
$table->text("description");
$table->timestamps();
});
The documentation is fairly thorough about this, and hasn't changed too much from version 3 to version 4.
Laravel 7
Create a migration file using cli command:
php artisan make:migration add_paid_to_users_table --table=users
A file will be created in the migrations folder, open it in an editor.
Add to the function up():
Schema::table('users', function (Blueprint $table) {
// Create new column
// You probably want to make the new column nullable
$table->integer('paid')->nullable()->after('status');
}
Add to the function down(), this will run in case migration fails for some reasons:
$table->dropColumn('paid');
Run migration using cli command:
php artisan migrate
In case you want to add a column to the table to create a foreign key constraint:
In step 3 of the above process, you'll use the following code:
$table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');
In step 4 of the above process, you'll use the following code:
// 1. Drop foreign key constraints
$table->dropForeign(['address_id']);
// 2. Drop the column
$table->dropColumn('address_id');
this things is worked on laravel 5.1.
first, on your terminal execute this code
php artisan make:migration add_paid_to_users --table=users
after that go to your project directory and expand directory database - migration and edit file add_paid_to_users.php, add this code
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('paid'); //just add this line
});
}
after that go back to your terminal and execute this command
php artisan migrate
hope this help.
First rollback your previous migration
php artisan migrate:rollback
After that, you can modify your existing migration file (add new , rename or delete columns) then Re-Run your migration file
php artisan migrate
WARNING: this is a destructive action. If you use this ensure you back up your database first.
You can simply modify your existing migration file, for example adding a column in your table, and then in your terminal typing :
$ php artisan migrate:refresh
Add column to your migration file and run this command.
php artisan migrate:refresh --path=/database/migrations/your_file_name.php
STEP 1
php artisan make:migration add_sex_to_users_table --table=users
STEP 2
In the newly generated migration file, you will find up and down hook methods. in up hook, add there columns that you want to add, and in down hook, add there columns that you need to remove. for example, Me i need to add sex on column of users, so I will add there following line in the up hook.
$table->integer('quantity')->default(1)->nullable();
So I have something like this
public function up()
{
Schema::table('service_subscriptions', function (Blueprint $table) {
$table->integer('quantity')->default(1)->nullable();
});
}
STEP 3
Run the migration command as follows
php artisan migrate
Then you will have a new column added
In laravel 8
php artisan make:migration add_paid_to_users_table --table=users
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
In laravel 9
add a new column in existing table
php artisan make:migration add_paid_to_users_table
if you want to create new migration then do the below code
php artisan make:migration create_users_table --create=users
First you have to create a migration, you can use the migrate:make command on the laravel artisan CLI.Old laravel version like laravel 4 you may use this command
for Laravel 4:
php artisan migrate:make add_paid_to_users
And for laravel 5 version
for Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
Then you need to use the Schema::table() . And you have to add the column:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
If you don't want to split the blueprint(schema) into two migration file then the best thing you can do is drop the table from the database and then rename the migration file's last number and do
php artisan migrate
This helps you to protect the data of other tables.
Run this command:
php artisan migrate:fresh --seed
it will drop the table and re add it updating all the columns adding to the database
What you can do is Like,
Schema::create('users', function ($table) { $table->integer("paid"); });
After Writing this write command php artisan migrate or php artisan refresh
What i personally prefer is to refresh rather than fresh migration because if you do fresh migrate it will remove all the data refresh will not.
but only exception is if you do refresh and if you have any foreign key in table so it will not going to re-establish the relationship so you will get error like,
Cannot add foreign key constrain
If none of the solve worked, you might have recreated the migration file then added a new column and tried to run php artisan migrate to update the old table which will try to create that table but the table already exists so it gives an error. To solve that rename the migration file as previously named (started with a date), then add new column run php artisan migrate that will actually update the old one instead of create, solved my problem.
Although a migration file is best practice as others have mentioned, in a pinch you can also add a column with tinker.
$ php artisan tinker
Here's an example one-liner for the terminal:
Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ $table->integer('paid'); })
(Here it is formatted for readability)
Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){
$table->integer('paid');
});