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

laravel 5.4 migration issue

I am having a very weird issue when running php artisan migrate on a new installation of Laravel 5.4 on a local XAMPP server with php 7.1.1. What it is doing is creating a migrations and users table, the default tables in older versions of…
Garret Corbett
  • 133
  • 1
  • 6
5
votes
2 answers

Laravel 5.4 migration ENUM fails in MySQL

when I try to apply my migration, I get this error: [Doctrine\DBAL\DBALException] Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it. The migration is applied, enum column is created on database and I…
Ivan Montilla
  • 392
  • 4
  • 19
5
votes
1 answer

Laravel 5.2 Migration: Cannot add foreign key of char data type

I am trying to create a nullable foreign key of char data type. When I run the migrate command. I get the following error. I am not sure, where I am doing wrong. [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add…
Kevin Reynolds
  • 129
  • 1
  • 9
5
votes
3 answers

Where to put migration within a Laravel 5.1 package?

Well, I do have a package take I only use alongside with my system. I do have migrations for that package (it was build on Laravel 4.2, and I'm upgrading it). That being said: On my package (former workbench) on Laravel 5.1, where do I put and how…
Dennis Braga
  • 1,448
  • 3
  • 20
  • 40
5
votes
2 answers

Update enum column in Laravel migration using PostgreSQL

According to this answer, I have to run a raw query if I want to update an enum in MySQL. But with PostgreSQL, I can't use this query, and enum type for PostgreSQL in Laravel seems strange. Is there any way to update enum in a migration for…
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
4
votes
2 answers

Laravel 9: how to insert data using raw sql while doing a migration?

I've a "static" table I need to import while doing initial migrations on my project. I've a .sql file. It's a lot of data, > 35k records My idea was to create table in a standard migration and then, with "something" execute the insert sql I already…
realtebo
  • 23,922
  • 37
  • 112
  • 189
4
votes
1 answer

Is it possible to change the datatype of a column from int to double without losing data with Laravel migration

I'm trying to change the datatype one of my column. My migration to create table was: public function up() { Schema::create('place_ratings', function (Blueprint $table) { $table->bigIncrements('id'); …
Sudipto Roy
  • 51
  • 1
  • 5
4
votes
6 answers

How to let ID autoincrement start from certain number in Laravel Migration

I want to write a Laravel Migration auto increment ID as a primary key. I want to start this ID with a another value rather than 1. How can I do so ? The migration up() function: public function up() { Schema::create('users', function…
4
votes
1 answer

Laravel Migration with Sqlite error: typeSet does not exist

TLDR; Within SQLite, is there a better or alternative way to use "set"? Full Information I am trying to run a migration within Laravel, which works fine on MySQL. However, whenever trying to run the same migration on a testing database on SQLite, I…
Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
4
votes
0 answers

How to use Laravel migrations in horizontally scaled setups with zero downtime?

I like to scale a web application horizontally, meaning having it run on multiple nodes and a load balancer distributing the load. In my case, it is a Laravel application, providing Laravel migrations for versioning database scheme changes. There is…
Richard Kiefer
  • 1,814
  • 2
  • 23
  • 42
4
votes
1 answer

How to create Laravel model from migration?

I found many results how to create migration from a model, but is there any way to create a model from migrations? Basically I want to give a table name, and if there is multiple migration files (one to create and others to update the previous…
netdjw
  • 5,419
  • 21
  • 88
  • 162
4
votes
2 answers

Laravel move data from one table to another and drop the column that the data came from?

I'm trying to do the following process in a Laravel migration: Create a new table called client_log_partitions.…
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
4
votes
1 answer

Add varchar column type with SQL Server using Laravel

I'm using Laravel 5.7 along with SQL Server 2017 and I want to generate a varchar(50) column called name. Executing this code gives me a nvarchar(50) instead: Schema::create('test', function(Blueprint $table) { $table->string('name',…
4
votes
1 answer

In Laravel migration, using string length larger than 255

I have an application on production, and I need to change length of a string column to 280 (as the default is 255). Is it safe for changing it to string to 280? In my local, using MySQLWorkbench, the string column was showing as VARCHAR(255) - I…
senty
  • 12,385
  • 28
  • 130
  • 260
4
votes
4 answers

How to add a random string column to laravel migration

I have a table called 'threads' in my laravel app, to which i want to add a new column named key with random strings of length 5. I ran the php artisan migrate:make add_key_to_threads on the CLL to create the migration file. In my generated…
banky
  • 838
  • 2
  • 13
  • 26