I'm fairy new to Laravel. I have created a migration file named: create_notes_table and when running command php artisan migrate this error message pops up.
my create_notes_table file content
class CreateNotesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->increments('id');
$table->number('card_id');
$table->body('string');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('notes');
}
}
As the error message states there is no body() method in migrations.
https://laravel.com/docs/8.x/migrations#columns
Change your function up() to :
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->increments('id');
$table->integer('card_id');
$table->string('body');
$table->timestamps();
});
}
While we're at it, number() also doesn't exist, changed it to integer()
Edit: changed number to integer
Related
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();
});
}
}
I've a Laravel application which has a users table with around 5000 records. The table has a column name which needs to be replaced with first_name and last_name.
With a simple migration script I updated the schema and wrote a simple artisan command to split name into first and last and restored in DB.
Now by rolling back the migration I lose the last_name column along with the data. What I'm looking for is that how can I run something after the execution of up method and before down method.
What would be the most appropriate way to achieve this?
Migration:
class ChangeColumnsInUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'first_name');
$table->string('last_name')->after('name');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('first_name', 'name');
$table->dropColumn('last_name');
});
}
}
Have you considered added the 'simple script' to the up() and down() functions of the migration itself? Something like this would work right?
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('first_name');
$table->string('last_name');
});
// Fill the first_name and last_name based on the name column
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('name');
});
}
The same can be done for the down() method, except the other way around.
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->text('comment');
$table->boolean('approved');
$table->integer('post_id')->unsigned();
$table->timestamps();
});
Schema::table('comments', function (Blueprint $table) {
$table->foreign('post_id')->references('id')->on('posts');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropForeign(['post_id']);
Schema::dropIfExists('comments');
}
}
This is what my migration class looks like, i have been trying to delete the table from the database but it throws me an error.
Error
Call to undefined method Illuminate\Database\Schema\MySqlBuilder::dropForeign()
I have gone through the documentation but it doesnot seem to be of much help.
could anyone please point out my mistake and what would be the solution ?
Just so you know , i am new to laravel.Go easy on me.Thanks!.
dropForeign needs to be called under Schema::table with a Blueprint object,
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign('comments_post_id_foreign');
});
This follows the naming convention of <table_name>_<foreign_table_name>_<column_name>_foreign.
OR
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign(['your_key_name']);
});
You're using wrong syntax. It should be:
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign('comments_post_id_foreign');
});
https://laravel.com/docs/5.3/migrations#foreign-key-constraints
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 have two types of workbench migrations: creating regular tables and creating pivot tables for Many-to-Many relationships.
Sample of regular migration:
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function(\Illuminate\Database\Schema\Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('login')->unique();
$table->string('username')->unique();
$table->string('password');
$table->string('email');
$table->boolean('active')->default(true);
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Above migration can be rolled back
<?php
use Illuminate\Database\Migrations\Migration;
class CreatePivotRoleUser extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('role_user', function(\Illuminate\Database\Schema\Blueprint $table)
{
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->primary(['role_id', 'user_id']);
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
While this cannot, because it gives
"Class 'CreatePivotPermissionRole' not found"
error.
How to fix it?
Your code looks correct.
If CreatePivotPermissionRole is not found, it means it had been deleted before. Check the content of all your down() methods, there must be something wrong there.