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
4
votes
0 answers

Laravel: One to Many Or Polymorphic Relation for Different Content types?

I have a table contents which has many types, for example posts , albums, videos. the contents table migration is : Schema::create('contents', function (Blueprint $table) { $table->increments('id'); $table->integer('ref_id')->index(); …
Ahmad Yousef
  • 621
  • 9
  • 20
4
votes
1 answer

How to add a constraint to a table with Laravel's migrations?

Let's say I have a table periods with columns start_date and end_date and I want to add a constraint start_date < end_date. The PostgreSQL query would be: ALTER TABLE periods ADD CONSTRAINT check_dates CHECK ("start_date" < "end_date"); But I want…
user1
  • 945
  • 2
  • 13
  • 37
4
votes
2 answers

Creating MYSQL FUNCTION in Laravel 4 Migrations

I am working on laravel 4.1. I want to create MYSQL function with migration but it doesn't work with php artisan migrate. even if it shows Migrated: 2017_01_10_140002_create_MYSQL_UNAVAILABLE_PRODS_FUNCTION in the console. and when i copy/past the…
Eissa
  • 325
  • 2
  • 11
4
votes
1 answer

Laravel 5 Testing with phpUnit without reset my database

Is there a way to make a setUp() and tearDown() method that tell laravel something like "make all of this insertions in Database, but when you finish all the tests, delete all insertions without reset the database" I need to do tests like this: /**…
4
votes
2 answers

How to create MyIsam table with a group auto_increment with Laravel migrations

Using Laravel Migrations, is there a way to create this an MyISAM table with a two-column primary key and an auto-increment in one of them? CREATE TABLE animals ( grp ENUM('fish','mammal','bird') NOT NULL, id MEDIUMINT NOT NULL…
4
votes
2 answers

How to define foreign keys in migrations?

The Problem I want to add foreign keys to tables. When I run my first migration create_posts_table that looks like this: Schema::create('posts', function(Blueprint $table) { $table->engine = 'InnoDB'; $table->increments('id'); …
Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
4
votes
1 answer

How to create table migration in Laravel 5

I am working on a project where, I have been assigned a task to create user management for the application. But I have stuck on the table relationship and their migration. Effort I have these…
Mandy
  • 1,103
  • 3
  • 17
  • 38
4
votes
2 answers

Laravel Migrations Deleted File

I'm working through Laracasts Laravel 5 Fundamentals however when coming to run a migration again I found that I had duplicate migrations at which point I figured - I should likely delete that. So I did... then began my issues. When I attempt to…
Kim Ward
  • 142
  • 1
  • 10
3
votes
2 answers

Change primary key in Laravel migration with SQLite

I have the following migration: Schema::create('items', function(Blueprint $table) { $table->uuid('id')->primary(); // more columns ... }); Now, we want to add an additional auto-increment column: Schema::table('items', function(Blueprint…
shaedrich
  • 5,457
  • 3
  • 26
  • 42
3
votes
2 answers

Laravel migration: using a native PHP enum

In PHP 8.1, native support for enums were introduced. How can I use them in a Laravel Migration? My first thought would be something like this, but it does not work. // migration public function up() { Schema::create('school_days',…
gfaster
  • 157
  • 2
  • 12
3
votes
1 answer

How are parallel test databases configured in Laravel?

From the Laravel Testing Documentation: # Parallel Testing & Databases Laravel automatically handles creating and migrating a test database for each parallel process that is running your tests. The test databases will be suffixed with a process…
Joel Mellon
  • 3,672
  • 1
  • 26
  • 25
3
votes
1 answer

Laravel database, what data type should I use to store a passport number?

I'm in the process of developing a Client Registration page for a travel agent. The client needs to save the passport number as a record in the MySQL database. I would like to know the data type ideal for mentioning in the migrations page for…
3
votes
1 answer

Laravel 8 Call to a member function extension() on null

I am trying to upload an image in Laravel 8, after changing the file path to public in the config folder and setting up the Model properly. I am getting this response Call to a member function extension() on null My Product controller public…
Adetola Aremu
  • 63
  • 1
  • 2
  • 6
3
votes
1 answer

Proper way to store 32-bit binary in Laravel

At the moment I'm using /** * Run the migrations. * * @return void */ public function up() { Schema::create('test_binary', function (Blueprint $table) { $table->increments('id'); $table->char('binary_number',…
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
3
votes
1 answer

How to use foreignId-constrained with default value?

It's just now that I noticed from the video tutorial that Laravel has this foreignId-constrained. So I try change my code from method.1 to method.2, the table migrated but when I try to db:seed, the default value is not working (it returns an error…
schutte
  • 1,949
  • 7
  • 25
  • 45