I have this migration:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
And now I want to add a new column, and looks like this:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('pathlogo')->default('/');
$table->timestamps();
});
}
How can I make just an 'add column' on Laravel? I don't want to do php artisan migrate:refresh, or restart and then make again seed.
Now I have some data in DB which not exist on seed I just want to make a new column.
You need to change Schema::create to Schema::table (because you are not creating a table, just selecting it), and then your only line in that function should be:
$table->string('pathlogo')->default('/')->after('slug');
after will ensure the column is positioned how you want.
If you are still in development, ie. you don't have data in the table, you should just rollback all your migrations and edit the original.
Create a new migration and Use that as your up function:
public function up()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('pathlogo')->default('/');
});
}
them migrate as normal.
Related
I'm on a Udemy course about Laravel 7. In a particular class, I need to rename the column of a table called site_contacts. When I try to simplify the code and I and put two instructions in a callback the error occurs: There is no column with name 'contact_reason_id' on table 'site_contacts'. This occurs when you reach the statement to change the column type. But, if I write separately, as it is in the code, migration runs without problems.
I would like to understand why it needs to be done this way and if there's any better way. Initially, I think the class needs to start with its updated internal attributes to proceed with the statements, otherwise it doesn't know that the column is under another name.
This code does not work.
public function up()
{
Schema::table('site_contacts', function (Blueprint $table) {
// There is no column with name 'contact_reason_id' on table 'site_contacts'.
$table->renameColumn('contact_reason', 'contact_reason_id');
$table->unsignedBigInteger('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->foreign('contact_reason_id')->references('id')->on('contact_reasons');
});
}
This code works.
public function up()
{
Schema::table('site_contacts', function (Blueprint $table) {
$table->renameColumn('contact_reason', 'contact_reason_id');
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->unsignedBigInteger('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->foreign('contact_reason_id')->references('id')->on('contact_reasons');
});
}
public function down()
{
Schema::table('site_contacts', function (Blueprint $table) {
$table->dropForeign('site_contacts_contact_reason_id_foreign');
$table->dropIndex('site_contacts_contact_reason_id_foreign');
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->integer('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->renameColumn('contact_reason_id', 'contact_reason');
});
}
Thanks.
I'm trying to pass the foreign key but am stuck with this error Base table or view already exists: 1050 Table 'favorites' already exists"
how can I pass them all the foreign keys and the normal id like $table->bigInteger('user_id')->unsigned();
tables
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->Increments('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
});
Schema::create('favorites', function (Blueprint $table){
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products');
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
You're migrating the table twice, pass the foreign keys in the same migration
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
Hope this helps
Other answers are correct. If ever you've missed the needed tasks before doing it, kindly try this one
1) Remove or delete the table you've migrated
2) In migrations table, delete the record which consists the name "favorites"
3) Copy and paste thi code inside the "up" function with this one
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
4) Run this command again
php artisan migrate
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('last_name')->nullable();
$table->string('phone_number')->nullable();
$table->text('Biography')->nullable();
$table->string('address')->nullable();
$table->string('photo')->nullable();
$table->string('facebook_link')->nullable();
$table->string('twitter_link')->nullable();
$table->string('youtube_link')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
I would like to changed the user to another name but could not and migration is not working.
If you want to rename the table, you can create a new migration with this code:
Schema::rename('users', 'some_other_table_name');
Then run composer du and php artisan migrate commands.
Or, you could recreate the table by rolling back this migration php artisan migrate:rollback whcih will work if you have this line in the down() method:
Schema::drop('users');
Change the table name:
Schema::create('some_other_table_name', function (Blueprint $table) {
And then run php artisan migrate
https://laravel.com/docs/5.5/migrations
I have the following migration:
public function up()
{
Schema::create('topics_to_subscriptions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('topic_id')->unsigned();
$table->integer('subscription_id')->unsigned();
$table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade');
$table->foreign('subscription_id')->references('id')->on('subscriptions')->onDelete('cascade');
});
}
My undersatnding is that when using onDelete('cascade'), if I delete a subscription, then all associated TopicsToSubscriptions will be delete.
When I run App\Subscription::truncate(); all the subscriptions are deleted correctly from subscriptions table but no data is deleted from topics_to_subscriptions. what am I doing wrong?
You shouldn't be able to truncate a table referenced by foreign keys. I suspect your foreign keys never got applied correctly.
https://laravel.com/docs/5.4/migrations#foreign-key-constraints
public function up()
{
Schema::create('youtube_topics_to_subscriptions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('topic_id')->unsigned();
$table->integer('youtube_subscription_id')->unsigned();
$table->foreign('topic_id')->references('id')->on('youtube_topics')->onDelete('cascade');
$table->foreign('youtube_subscription_id')->references('id')->on('youtube_subscriptions')->onDelete('cascade');
});
}
My migration is like this :
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id_test');
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
I want to change data type and add column in table test. Then update table in database. I edit like this :
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->string('id_test', 18);
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
Then I run : php artisan migrate:refresh
I lost my data in database
Is there any people who can help me?
Create a new migration file using below command :
php artisan make:migration name_of_your_file
And its function should be like this
public function up()
{
Schema::table('tests', function($table) {
$table->renameColumn('id_test', 'id');
$table->string('id_test', 18);
});
}
public function down()
{
Schema::table('tests', function($table) {
$table->dropColumn('id_test');
});
}
Now just run below command
php artisan migrate
Note : Dont use refresh it deletes the data
To know more about Laravel Migration refer : https://laravel.com/docs/5.3/migrations#modifying-columns