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
21
votes
3 answers

Laravel timestamps() doesn't create CURRENT_TIMESTAMP

I have a migration that has the timestamps() method, and then I have a seed to seed this table. Schema::create('mytable', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->timestamps(); }); The…
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
18
votes
3 answers

Available actions for onUpdate / onDelete in Laravel 5.x

As mentioned in here, we can use the word cascade when making a relation in migrations but I wonder they didn't say anything about other actions when deleting or updating a foreign key so I'm not sure if there is such thing or…
bobD
  • 1,037
  • 2
  • 9
  • 17
17
votes
2 answers

Laravel: migrations change default value(boolean) of column

This is my current code. public function up() { Schema::table('render', function (Blueprint $table) { $table->boolean('displayed')->default(1); }); }` How to change the default value to 0 as below? public function up() { …
Tomeikun
  • 309
  • 1
  • 2
  • 5
17
votes
3 answers

Changing Laravel MYSQL to utf8mb4 for Emoji Support in Existing Database

I am using Laravel 5.3 and I have already set-up my production server. All DB Migrations were already created using the following database config: 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), …
Neel
  • 9,352
  • 23
  • 87
  • 128
16
votes
1 answer

Laravel Migration: Add a column with a default value of an existing column

i need to add a last_activity column on a table of a live site where i have to make it's default value to equal of updated_at column value. I'm using laravel 5.1 and mysql database driver. Schema::table('users', function(Blueprint $table) { …
Parvez Rahaman
  • 4,269
  • 3
  • 22
  • 39
16
votes
1 answer

Laravel 5.1 How do you create a MySQL stored procedure with migration

Hello I'm trying to create a stored procedure using Laravel 5.1 migrations. So far I tried with DB::connection()->getPdo()->exec(), DB::raw() and DB::unprepared(), but no luck. Here's how my code looks like: use…
15
votes
3 answers

How do Laravel migrations work?

I'm totally new to this type of framework. I've come from barebones PHP development and I can't seem to find an easy to understand guide what migrations actually do. I'm trying to create a project that already has an existing database. I've used…
Recur
  • 1,418
  • 2
  • 17
  • 30
14
votes
4 answers

How to specify folder for laravel migrations?

I am trying to put laravel database migrations in sub folders, but not sure how to do this.
Veljko Sirovljević
  • 199
  • 1
  • 1
  • 13
14
votes
2 answers

Laravel migration - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I am trying to run a migration for a table inventories that I have created with this migration: Schema::create('inventories', function (Blueprint $table) { $table->increments('id'); $table->integer('remote_id')->unsigned(); …
Leff
  • 1,968
  • 24
  • 97
  • 201
13
votes
4 answers

Is it possible to do migration on dynamic database connection with Laravel 5?

I am trying to dynamically create database for different users. (every user will have their own database server, so don't ask why I am not using a single database for all users) To do that, I have a default database storing all the connection…
cytsunny
  • 4,838
  • 15
  • 62
  • 129
13
votes
3 answers

How to add comment to table (not column) in Laravel 5 migration?

How to add comment to table (ot column) in Laravel 5 migration? I currently know how to add comment to column like: $table->tinyInteger('status')->comment('0: requested; -1: rejected; 1:confirmed'); But what about table?
mohsenJsh
  • 2,048
  • 3
  • 25
  • 49
12
votes
6 answers

How to run laravel migration and DB seeder except one

I am having many migration and seeder files to run, Although I will need to run all files but currently I need to skip one migration and seeder. How could I skip one file from laravel migration and db seeder command. I do not want to delete files…
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
11
votes
2 answers

Using UUID as default value in a laravel migration

I have the following migration:
Aleksej_Shherbak
  • 2,757
  • 5
  • 34
  • 71
11
votes
5 answers

Can't start Laravel, I get "Base table or view not found" error

First I rolled back 2 migrations by mistake, then I ran php artisan migrate command and I got the following error: [Illuminate\Database\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist…
Mahammad Isgandarli
  • 528
  • 1
  • 7
  • 17
9
votes
1 answer

How to run laravel migrations without artisan (using code)

I recently hosted a laravel project (for a customer) on shared hosting, after failed attempts to get access to the server via ssh I contacted the host who informed me that ssh service was not available for my customers hosting plan, that means I…
Fenn-CS
  • 863
  • 1
  • 13
  • 30
1
2
3
32 33