I am developing on Laravel Framework 8.33.1 and have the following migration on my local environment and also in production.
class CreateCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('company', function (Blueprint $table) {
$table->id();
$table->integer('company_id');
$table->integer('messageId');
$table->string('url');
$table->timestamps();
/**
New table:
$table->id();
$table->integer('company_id')->nullable($value = true);
$table->integer('messageId');
$table->integer('people_id')->nullable($value = true);
$table->string('url')->nullable($value = true);
$table->timestamps();
*/
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('company');
}
}
I would like to change the migration with the following adapted fields:
$table->id();
$table->integer('company_id')->nullable($value = true);
$table->integer('messageId');
$table->integer('people_id')->nullable($value = true);
$table->string('url')->nullable($value = true);
$table->timestamps();
As I am using the current migration in production I do not want to lose data.
I simply tried to modify the migration file with my new table definition, but I get:
> php artisan migrate
Nothing to migrate.
Any suggestions how to change the migration properly in laravel?
I appreciate your replies!
To modify an existing table, create a new migration.
php artisan make:migration alter_company_table
class AlterCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable()->change();
$table->integer('people_id')->nullable();
$table->string('url')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable(false)->change();
$table->dropColumn('people_id');
$table->string('url')->nullable(false)->change();
});
}
}
If you don't mind losing data and you are in development mode, you can do php artisan migrate:rollback and after execute: php artisan migrate. So your migrations works correctly. You can to read more here.
Now, if you just want to add new tables and modify others in you database, you should make another migration. But before this, you should to install doctrine/dbal to be able to modify your tables correctly, or else, it will give you many errors. After, add this lines in your config/database.php
use Illuminate\Database\DBAL\TimestampType;
'dbal' => [
'types' => [
'timestamp' => TimestampType::class,
],
],
Now you can do: php artisan make:migration add_new_tables_to_company and your file in database/migrations.
class AlterCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable()->change();
$table->integer('people_id')->nullable();
$table->string('url')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable(false)->change();
$table->dropColumn('people_id');
$table->string('url')->nullable(false)->change();
});
}
}
Related
I've got a task to create a migration for adding 2 instances of WorkRoom into database, I've already create a migration when created the Model and added to table 3 fields, I don't understand what migration should I do now.
class CreateWorkRoomsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('work_room', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->integer('building')->nullable();
$table->integer('floor')->nullable();
$table->integer('office_number')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('work_room');
I use Laravel 5.2, Php7, Apache, Windows
my migration file is "2016_03_30_095234_alter_date_update_news_table.php"
class AddSlugUpdateNewsTable extends Migration
{
/**
* Run the migrations.
* php artisan make:migration add_slug_update_news_table
* #return void
*/
public function up()
{
Schema::table('news', function (Blueprint $table) {
$table->date('slug')->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('news', function (Blueprint $table) {
$table->dateTime('slug')->change();
});
}
}
But after run migrate,
$\> php artisan migrate
gives me this error!
[RuntimeException] Changing columns for table "news" requires
Doctrine DBAL; install "doctrine/dbal".
what do i do?
according to laravel docs. You must install doctrine/dbal first if you are using ->change() function.
Type composer require doctrine/dbal in your terminal
/**
* Run the migrations.
* php artisan make:migration alter_date_update_news_table
* #return void
*/
public function up()
{
Schema::table('news', function (Blueprint $table) {
$table->dropColumn('date');
});
Schema::table('news', function (Blueprint $table) {
$table->date('date')->after('image');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('news', function (Blueprint $table) {
$table->dropColumn('date');
});
Schema::table('news', function (Blueprint $table) {
$table->dateTime('date')->after('image');
});
}
Seems like you're missing a dependency.
Install it with composer:
composer require doctrine/dbal
I created a users table using Laravel's schema builder. Here is the migration:
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::connection('mysql-internal')->create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::connection('mysql-internal')->drop('users');
}
}
After running this migration, I then rolled it back using php artisan migrate:rollback. However, it looks like down method did not respect my connection. The users table was dropped from my default mysql database rather than the mysql-internal that I specified.
Did I do something wrong here? I am using Laravel 4.2.17.
When I run php artisan migrate i get error B
Base table or view already exists: 1050 Table 'categories' already
exists'
What is it? Why? How to find error?
My categories migrations file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->index();
$table->text('description');
$table->integer('attachment_id')->unsigned()->index();
$table->foreign('attachment_id')->references('id')->on('attachment')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
In that migration file, the function drop() should be
public function down()
{
Schema::drop('categories');
}
It seems that you try to create existing table..
so i think you are editing the migration file that you already installed
you can drop your database and re-migrate it
php artisan migrate:reset
then
php artisan migrate
So, I have created a new package that sites in vendor/vendorname/package
I have added the new package to my app/config/app.php file's provider array:
'Seriousjelly\Portfolio\PortfolioServiceProvider'
My package composer.json file seems legit:
"autoload": {
"classmap": [
"src/migrations",
"src/controllers",
"src/models",
"src/repositories"
],
"psr-0": {
"Seriousjelly\\Portfolio\\": "src/"
}
And in seriousjelly/porfolio/src/migrations I have 2 migrations:
2014_06_25_060429_create_portfolio_categories_table.php:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePortfolioCategoriesTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('portfolio_categories', function(Blueprint $table){
$table->increments('id');
$table->string('title', 255);
$table->string('subtitle', 255);
$table->string('short_description', 500);
$table->string('description');
$table->string('slug', 60);
$table->timestamp();
});
Schema::create('portfolio_item_categories', function(Blueprint $table) {
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('portfolio_categories');
$table->integer('item_id')->unsigned();
$table->foreign('item_id')->references('id')->on('portfolio_items');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('portfolio_categories');
Schema::drop('portfolio_item_categories');
}
}
and 2014_06_25_060414_create_portfolio_items_table.php:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePortfolioItemsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('portfolio_items', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 255);
$table->string('subtitle', 255)->nullable;
$table->string('short_description', 500)->nullable;
$table->string('description', 500);
$table->string('slug', 60);
$table->timestamp();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('portfolio_items');
}
}
Now, when I run php artisan migrate --package=seriousjelly/portfolio
It comes up saying that the migration ran successfully, however, if I check the database only the migrations table has been populated.
If I run the command once more, nothing happens, no errors, no message, nothing.
Any ideas?
p.s other package migrations are the same and seem to run ok, I have attempted composer update and composer dump-autoload but this doesn't work.
Ah, ok my bad. It seems that I have incorrectly spelt, turns out it's plural:
$table->timestamps();
Im not sure why terminal never threw an error though...