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
2
votes
2 answers

Field 'id' doesn't have a default value for UUID Field in Laravel

I have just started learning laravel. I'm familiar with CakePHP. I used to use UUID field as primary key in my database and in CakePHP, it was quite simple to just change data type of column field to CHAR(36) and it works well. In Laravel, I have…
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
2
votes
2 answers

Not able to add foreign key constraint to pivot table

I am trying to create a pivot table, with foreign keys, this is the migration I have made in Laravel: public function up() { Schema::create('player_position', function (Blueprint $table) { …
Leff
  • 1,968
  • 24
  • 97
  • 201
2
votes
2 answers

Foreign key is incorrectly formed

When I changed my primary key from increments to string (user_id) i now got an error in my migrations. I dont seem to see where I went wrong. my migrations files were okay until I changed from (->increments and the other one ->integer) to (->string…
Ralph
  • 193
  • 1
  • 4
  • 12
2
votes
1 answer

if migration failed then automatic rollback

i am trying to migrate some LARAVEL migration by php artisan migrate command and if there will be a problem in migration it shows error/exception or any other thing. the migration successfully runs I need them to roll back automatically is there any…
M Amir Shahzad
  • 190
  • 2
  • 16
2
votes
1 answer

Laravel 5.3, MySQL, proper foreign key setup in migrations (one to many, many to many)

what is the proper way to setup the foreign key restrains in following scentarios. Let say I have models Post, User, Tag. At the moment my migrations are all set simply as: For Comment model: $table->integer('posts_id')->unsigned(); //comment must…
dbr
  • 1,037
  • 14
  • 34
2
votes
2 answers

Removing default migrations from Laravel

After creating a new Laravel project with laravel new, there are a couple of migration files in the database > migrations folder: 2014_10_12_000000_create_users_table.php 2014_10_12_100000_create_password_resets_table.php These seem to be examples…
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
2
votes
1 answer

How to maintain two migration tables in Laravel Migration

I'm writing a custom migration and I need to maintain those migrations in a separate migration repository table. I override DatabaseMigrationRepository and replace the migration repository function as follows public function registerRepository() { …
Mohamed Nizar
  • 780
  • 9
  • 30
2
votes
3 answers

Laravel 5.4 migrate:generate fails with Way\Generators\Filesystem\FileNotFound error

I am trying to use Xethron/migrations-generator in a Laravel 5.4 project in order to generate migration files for all of the tables in my database. I followed the instructions in the README file for Laravel 5 to the letter. After resolving a…
S. Imp
  • 2,833
  • 11
  • 24
2
votes
1 answer

Laravel - php artisan migrate doesn't work

I'm trying to migrate my auth tables but when i do php artisan migrate nothing happens. It shows no error, nothing. Before run this command i ran php artisan make:auth and work well. Thanks
2
votes
1 answer

Laravel - Migration (Adding indexes to tables throws an error)

I have made a migration file where I am adding indexes to existing columns in my database. This is my migration file: public function up() { Schema::table('alternatives', function (Blueprint $table){ …
Ludwig
  • 1,401
  • 13
  • 62
  • 125
2
votes
2 answers

What is the best way to change a column type in Laravel migration?

I have a User table in my DB: $table->increments('id'); $table->string('fullName'); $table->string('email')->unique(); $table->string('password', 50); …
Mo-
  • 790
  • 2
  • 10
  • 23
2
votes
3 answers

Eloquent relations and database constraints?

since Eloquent supports relationships, would you recommend to additionally define foreign key constraints in the migrations? Why? Thanks
alexandre
  • 286
  • 3
  • 17
2
votes
3 answers

Laravel seeding - unique pairs of user and teacher IDs

I am using database migration and seeding in Laravel 5.1. Migration public function up() { Schema::create('teachers', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); …
Andrew
  • 189
  • 1
  • 4
  • 18
2
votes
2 answers

Convert MySQL view to Postgres

I've inherited the need to convert a production MySQL DB over to Postgres. This has mostly been handled without issue using simple SQL statements for table/function creation (using Navicat to generate semi-automated conversion), but now I am hitting…
Daniel
  • 685
  • 1
  • 6
  • 13
2
votes
1 answer

Should I gitignore seeder files in Laravel 5?

I've setup my migration files and my seeder files with fake data and they're working nicely. I'm about to commit my work but don't really know what to do with my seeder files. I definitely don't want to run seeder files in production, so I thought I…
Vic
  • 2,655
  • 3
  • 18
  • 28