Questions tagged [laravel-migrations]

Database Migrations in Laravel are the way to create and alter database schema.

Introduction

Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema.

Creating Migrations

To create a migration, you may use the make:migration command on the Artisan CLI:

php artisan make:migration create_users_table

The migration will be placed in your database/migrations folder, and will contain a timestamp which allows the framework to determine the order of the 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:

php artisan make:migration add_votes_to_users_table --table=users

php artisan make:migration create_users_table --create=users

Running Migrations

Running All Outstanding Migrations

php artisan migrate

Note: If you receive a "class not found" error when running migrations, try running the composer dump-autoload command.

Forcing Migrations In Production

Some migration operations are destructive, meaning they may cause you to lose data. In order to protect you from running these commands against your production database, you will be prompted for confirmation before these commands are executed. To force the commands to run without a prompt, use the --force flag:

php artisan migrate --force

Rolling Back Migrations

Rollback The Last Migration Operation:

php artisan migrate:rollback

Rollback all migrations:

php artisan migrate:reset

Rollback all migrations and run them all again:

php artisan migrate:refresh

php artisan migrate:refresh --seed

For more information visit Laravel migrations reference.

489 questions
475
votes
20 answers

Laravel Add a new column to existing table in a migration

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...
kim larsen
  • 5,111
  • 6
  • 19
  • 17
318
votes
12 answers

Laravel Migration Change to Make a Column Nullable

I created a migration with unsigned user_id. How can I edit user_id in a new migration to also make it nullable()? Schema::create('throttle', function(Blueprint $table) { $table->increments('id'); // this needs to also be nullable, how…
user391986
  • 29,536
  • 39
  • 126
  • 205
238
votes
6 answers

Laravel Unknown Column 'updated_at'

I've just started with Laravel and I get the following error: Unknown column 'updated_at' insert into gebruikers (naam, wachtwoord, updated_at, created_at) I know the error is from the timestamp column when you migrate a table but I'm not using…
Loko
  • 6,539
  • 14
  • 50
  • 78
170
votes
8 answers

Laravel Schema onDelete set null

Can't figure out how to set proper onDelete constraint on a table in Laravel. (I'm working with SqLite) $table->...->onDelete('cascade'); // works $table->...->onDelete('null || set null'); // neither of them work I have 3 migrations, creating the…
M K
  • 9,138
  • 7
  • 43
  • 44
94
votes
15 answers

Running one specific Laravel migration (single file)

I have 5 migrations in my project. I only want to run one of these migrations. Is it possible to pass the name of a single file to the php artisan migrate command?
Mamadou
  • 2,177
  • 5
  • 31
  • 43
92
votes
6 answers

Laravel migrations change a column type from varchar to longText

I need to change with a migration column type of $table->string('text'); to a text type, I have tried to do that in a few ways, but none of them worked. Is it possible to do it in one migration? I could I guess drop the column and then create it…
Ludwig
  • 1,401
  • 13
  • 62
  • 125
86
votes
11 answers

Error migrations: Cannot declare class X, because the name is already in use

I do not know why this error occurs when I execute the migrations as I do not have repeated…
Federico Fia Sare
  • 1,166
  • 2
  • 8
  • 15
70
votes
6 answers

Add sql table column before or after specific other column - by migrations in Laravel 4.1

Table 'users': |id|name|address|post_code|deleted_at|created_at| and I want add column 'phone_nr' somewhere between 'id' and 'deleted_at' Is it possible by migrations in Laravel 4.1?
Lajdák Marek
  • 2,969
  • 8
  • 29
  • 58
60
votes
5 answers

How to convert Laravel migrations to raw SQL scripts?

Developers of my team are really used to the power of Laravel migrations, they are working great on local machines and our dev servers. But customer's database admin will not accept Laravel migrations. He asks for raw SQL scripts for each new…
JustAMartin
  • 13,165
  • 18
  • 99
  • 183
57
votes
5 answers

Artisan, creating tables in database

I am trying to create mysql tables in Laravel 5. I created a file in /project/database/migrations called users.php: [...] public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); …
Streetlamp
  • 1,537
  • 2
  • 15
  • 27
52
votes
1 answer

How to drop softDeletes from a table in a migration

I'm adding the soft delete columns to my table in a migration: public function up() { Schema::table("users", function ($table) { $table->softDeletes(); }); } But, how can I remove these in my down() function, if I roll back the…
miken32
  • 42,008
  • 16
  • 111
  • 154
51
votes
1 answer

naming tables in many to many relationships laravel

I concerned about auto naming tables in many-to-many Laravel relationship. for example: Schema::create('feature_product', function (Blueprint $table) {} when change the table name to: Schema::create('product_feature', function (Blueprint $table)…
Pedram marandi
  • 1,474
  • 1
  • 19
  • 35
46
votes
7 answers

How to seed database migrations for laravel tests?

Laravel's documentation recommends using the DatabaseMigrations trait for migrating and rolling back the database between tests. use Illuminate\Foundation\Testing\DatabaseMigrations; class ExampleTest extends TestCase { use…
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
46
votes
2 answers

Drop Unique Index Laravel

I kept getting this while run php artisan migrate SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'email'; check that column/key exists While I see that email is exist on my database. My migration script. I was trying to drop…
code-8
  • 54,650
  • 106
  • 352
  • 604
43
votes
9 answers

Laravel 5.1 Migration and Seeding Cannot truncate a table referenced in a foreign key constraint

I'm trying to run the migration (see below) and seed the database, but when I run php artisan migrate --seed I get this error: Migration table created successfully. Migrated: 2015_06_17_100000_create_users_table Migrated:…
mtpultz
  • 17,267
  • 22
  • 122
  • 201
1
2 3
32 33