update table column without reseting the database migrate Laravel - laravel

I want to add new column or delete column to a table in laravel migration and I don't want to reset the database because it delete all old data.

make a new migration then the code should be like
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->nullable()->change();
});
or if you want to rename
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
or if dropping
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['votes', 'avatar', 'location']);
});
to add simply
Schema::table('users', function (Blueprint $table) {
$table->string('email');
});

Related

Laravel - How to add foreign key reference to already existing migration

In my Larave-8, I created a table using this migration:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id');
$table->string('first_name',50);
$table->string('last_name',50);
$table->string('other_name',50)->nullable();
$table->string('gender',20);
$table->string('user_photo',350)->nullable();
$table->integer('nationality_id');
$table->string('marital_status',50);
$table->date('dob')->nullable();
$table->string('address', 300)->nullable();
$table->integer('country_id')->nullable();
$table->integer('state_id')->nullable();
$table->integer('city_id')->nullable();
$table->string('cv_file',350)->nullable();
$table->text('summary')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
How do I alter the table through migration and add these:
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
Thanks
create another migration like to alter existing table
public function up()
{
Schema::table('profiles', function (Blueprint $table) {
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
});
}
public function down() {
Schema::table('profiles', function (Blueprint $table) {
$table->dropForeign('nationality_id');
$table->dropForeign('country_id');
$table->dropForeign('state_id');
$table->dropForeign('city_id');
});
}

Having problems with Laravel Migrations

I have added all my in constraints 2020_06_17_221942_create_constraints_table
but its not working
All migrations successfully done but without any table constraints the 2020_06_17_221942_create_constraints_table is :
Schema::table('seasons', function (Blueprint $table) {
$table->unsignedBigInteger('best_project_id');
$table->unsignedBigInteger('manager_id');
$table->foreign('best_project_id')->references('id')->on('projects');
$table->foreign('manager_id')->references('id')->on('users');
});
Schema::table('admins', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onDelete('cascade');;
});
Schema::table('projects', function (Blueprint $table) {
$table->foreign('study_major_id')->references('id')->on('study_majors');
$table->foreign('user_id')->references('id')->on('Users');
$table->foreign('season_id')->references('id')->on('Seasons');
})
Try adding "Blueprint" type-hinting to the migration function. It should work
For example:
Schema::table('seasons', function (Blueprint $table) {
$table->unsignedBigInteger('best_project_id');
$table->unsignedBigInteger('manager_id');
$table->foreign('best_project_id')->references('id')->on('projects');
$table->foreign('manager_id')->references('id')->on('users');
});
Replace your last query with this
Schema::table('projects', function (Blueprint $table) {
$table->unsignedBigInteger('study_major_id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('season_id');
$table->foreign('study_major_id')->references('id')->on('study_majors');
$table->foreign('user_id')->references('id')->on('Users');
$table->foreign('season_id')->references('id')->on('Seasons');
});

Laravel 7 General error: 1215 Cannot add foreign key constraint

I've been researching this for hours, can't seem to solve it. Here is the error
General error: 1215 Cannot add foreign key constraint (SQL: alter table 'users' add constraint 'users_discord_id_foreign' foreign key ('discord_id') references 'discord_o_auths' ('id'))
Here is my DiscordOAuths Migration:
public function up()
{
Schema::create('discord_o_auths', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->integer('id')->unique();
$table->string('access_token')->unique();
$table->string('refresh_token')->unique();
$table->bigInteger('token_expiration');
$table->timestamps();
});
}
And here is my Users Migration.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->id();
$table->boolean('admin')->default(false);
$table->integer('color_scheme')->default(0);
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function($table) {
$table->engine = "InnoDB";
$table->integer('discord_id')->unsigned();
$table->foreign('discord_id')->references('id')->on('discord_o_auths');
});
}
The DiscordOAuths table is created before the Users table as well. What am I doing wrong?
You're trying to assign a foreign key constraint to an integer column, which is not the same as the unsigned columns that Laravel uses for its id's.
In your DiscordOAuths you have to replace $table->integer('id')->unique(); with $table->bigIncrements('id'); or the new method added in Laravel 7 $table->id(); which is an alias of the previous one.
Your discord_id column in the users migration also needs to be an unsigned column. You have to define it using either $table->unsignedBigInteger('discord_id'); or its alias that came with Laravel 7: $table->foreignId('discord_id');
So your migrations would look like this:
For DiscordOAuths
public function up()
{
Schema::create('discord_o_auths', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->id();
$table->string('access_token')->unique();
$table->string('refresh_token')->unique();
$table->bigInteger('token_expiration');
$table->timestamps();
});
}
And for users it would be like this:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->id();
$table->boolean('admin')->default(false);
$table->integer('color_scheme')->default(0);
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function($table) {
$table->engine = "InnoDB";
$table->foreignId('discord_id');
$table->foreign('discord_id')->references('id')->on('discord_o_auths');
});
}

how to add a new column between 2 columns in laravel

migration file
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('content');
$table->string('short_description');
$table->unsignedInteger('media_id')->nullable();
$table->foreign('media_id')->references('id')->on('medias');
$table->unsignedInteger('creator_id');
$table->foreign('creator_id')->references('id')->on('users');
$table->boolean('hide')->default(0);
$table->timestamps();
});
}
after hide column i want to add 'privacy column'
Either add it to the migration file or create another migration like this :
Schema::table('posts', function (Blueprint $table) {
$table->string('privacy')->after('hide');
});

Laravel relationship error in migration

I am trying to make two tables relationship in laravel. But i can't understand why i'm getting error "errno: 150 "Foreign key constraint is incorrectly formed"". please check my migration code below.
items table
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->string('name');
$table->decimal('price',5,2);
$table->integer('count_left')->default(0);
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories');
});
}
categories
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}

Resources