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
0
votes
1 answer

Laravel DB Table Model Implementation in Pure PHP

I have worked with some Laravel projects in the past. Creating a table and using them in a controller is pretty easy. However, I mostly work with pure PHP. Most of them are not class object based. I want to write code to work similar to the Laravel…
Mobile Dev
  • 49
  • 1
  • 9
0
votes
1 answer

Laravel migrate:refresh Doesn't Work After Composer Update

The command in the title return error message below: Type error: Too few arguments to function Illuminate\Database\Schema\Builder::create(), 1 passed in …
Aslam H
  • 1,669
  • 4
  • 21
  • 46
0
votes
3 answers

1215 Can not add foreign key

Larvel 5.6.3 PHP 7.2.10 I am getting this following error for php artisan migrate:fresh General error: 1215 Cannot add foreign key constraint (SQL: alter table `videos` add constraint `videos_video_identified_by_foreign` foreign key…
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0
votes
0 answers

Laravel Eloquent DB Migration

We want to count the duration between two different date fields on a database. Can anyone help convert this SQL query to a database migration in Laravel? CREATE TABLE api_incident_duration (START DATETIME, END DATETIME, elapsed DECIMAL(5, 2) AS…
user7678643
0
votes
1 answer

Change foreign key without losing data in column

I've checked google, but I haven't found a way to preserve data while changing a foreign key on a table. I have two tables, User Events and User Sports Table User Events +--------------+------------------+------+-----+---------+----------------+ |…
Goran Culibrk
  • 63
  • 2
  • 10
0
votes
3 answers

I can’t add new columns to the table from laravel even after following the procedure of migration?

I am following all the steps for adding new column votes to the user table from laravel, still there isn't in the database? Please tell me where is my mistake? Firstly php artisan make:migration add_votes_to_users_table --table=users public function…
Akeedify
  • 23
  • 7
0
votes
1 answer

How to swap two indexed fields with laravel migrations?

In the Models of a many to many relationship I have accidentally identified the foreign key names in reverse. This is done in both related Models so the relationship works. It's in production. In Articles: public function categories() { return…
Dimitri Mostrey
  • 2,302
  • 1
  • 12
  • 11
0
votes
1 answer

Laravel Migrations: Cannot add foreign key constraint

I am trying to add foreign key constraints to my database tables via Laravel Migrations, but I always get an error like this: Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter…
0
votes
4 answers

@Laravel Migration: Cannot add foreign key constraint in laravel

I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error: λ php artisan migrate Migration table created successfully. [Illuminate\Database\QueryException] SQLSTATE[HY000]: General…
0
votes
1 answer

Laravel strings Foreign Key incorrectly formed

I'm trying to create a fk between tow tables. Here are my migrations: public function up() { Schema::create('contracts', function (Blueprint $table) { $table->increments('id'); // Some other cols removed for this…
Markus
  • 1,909
  • 4
  • 26
  • 54
0
votes
1 answer

Did a migration to database (hrm-master) and got this errors

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hrm-master.sessions' doesn't exist (SQL: select * from sessions where id = FCtzXvm1CTIvGJCfWuTVyO1bpyDIDVnt9FlmoCAY limit 1) in Connection.php line 647 at…
jokova
  • 13
  • 3
0
votes
2 answers

How to add one column to a DB table with Laravel migrations?

I'm using Laravel migration files to help set up a MariaDB schema for my project. It works fine for the initial table setup of the schema, but after it has been created, how (if it's even possible at all) can I add one field to a migration file and…
HartleySan
  • 7,404
  • 14
  • 66
  • 119
0
votes
1 answer

Laravel migrations table is MyISAM table format

By default the laravel migrations table is using MYIsam engine. I have changed engine in config/database.php to InnoDB and all my other tables except migrations uses InnoDB
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
0
votes
1 answer

Laravel showing 1215(Cannot add foreign key constraint) when using migration:fresh through ssh

Laravel 5.6.38 MySQL 5.7.23 PHP v7.2.10 In localhost PHP 7.2.4, It is working fine in localhost but in production it is showing below error. php artisan migrate:fresh Dropped all tables successfully. Migration table created successfully. …
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0
votes
1 answer

Editing user info using laravel

I've built a cms interface for the admin in my website. among other things the admin can add\edit users info using forms. when I send the edit form I keep getting this error: Column not found: 1054 Unknown column 'updated_at' in 'field list' which…
TheProphet
  • 89
  • 1
  • 9