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

where is $table->stamps() documented and how do you roll it back?

So at my job we have a project using https://github.com/WildSideUK/Laravel-Userstamps . In existant migrations there's $table->stamps(); which creates created_at, deleted_at, created_by, etc. This method is not documented at…
neubert
  • 15,947
  • 24
  • 120
  • 212
0
votes
1 answer

Adding new enum type to the existing enum column in Laravel migration

I am developing a Laravel application. What I am trying to do now is that I am trying to add a new enum type to the existing enum column. This is how enum column is created/ migrated in the first place. $table->enum('type', [BlogType::IMAGE,…
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
0
votes
2 answers

Multiple Auto-Incrementing Columns For Laravel

I am trying to create a second auto-incrementing column called order for my table called posts. I am creating this auto-incrementing column because I am using jQuery sortable, and you can change the order of posts. But unfortunately, I get this…
Tyler Shannon
  • 279
  • 1
  • 4
  • 16
0
votes
1 answer

Pivot table to check user's availability

I'm making a jobs website. Clients need to be able to search(checkbox input) the availability of contractors for certain days of the week. I'm thinking about making a pivot table to store availability. ID column representing days of the week,…
0
votes
1 answer

Laravel migration can not add foreign key in user table

I want to add a foreign key in the users table. Everything seems ok yet I get the following error. SQLSTATE[HY000]: General error: 1005 Can't create table electro_service.#sql-17d0_20 (err no: 150 "Foreign key constraint is incorrectly formed")…
virtual
  • 3
  • 4
0
votes
1 answer

How to remove all migrations from a Laravel project but keep the tables and fields

I've decided to remove all the migrations from a Laravel 5.7 project as I would rather create/update the tables by hand. I've realised that data can still be loaded from the database without any migration files.
David Johnson
  • 153
  • 1
  • 1
  • 8
0
votes
1 answer

Laravel Migration onDelete() Query Exception

I have a simple laravel migration with a foreign key. public function up() { Schema::create('smp_posts', function (Blueprint $table) { $table->increments('id'); $table->integer('project_id')->unsigned(); // Some other…
Markus
  • 1,909
  • 4
  • 26
  • 54
0
votes
0 answers

Trying to run migration on Laravel getting an error

Hey just started to use Laravel, however, cannot run migration. I followed the documentation on laravel website and installed XXAMP. The server is running and when i type http://192.168.64.2/phpmyadmin it takes me to phpmyadmin dashboard. Also…
bisamov
  • 650
  • 1
  • 8
  • 24
0
votes
3 answers

Not able to see foreign keys in PHPMyAdmin created using Laravel migration

I'm trying to create a simple table with a foreign key referencing the default "users" table of Laravel. Migration ran successfully, but when I see the newly created table in PHPMyAdmin, it doesn't show the foreign key. Here is my simple migration…
Parth Vora
  • 4,073
  • 7
  • 36
  • 59
0
votes
3 answers

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint Laravel 5.8

I am trying to make a foreign key to "users" table using Laravel 5.8. Laravel 5.8 auto generated migration table is as follows, public function up() { Schema::create('users', function (Blueprint $table) { …
user3581438
  • 350
  • 1
  • 4
  • 8
0
votes
2 answers

How can i set already migrated migrations as a migrated migrations in Laravel without migrate:fresh?

I have 20 tables in my database. and i want to migrate a new one into the database. But i get the error : Base table or view already exists: 1050 Table 'house_statuses' already exists. This table is already in database but everytime i want to…
0
votes
1 answer

Limiting text column characters in Laravel 5

I have a Laravel 5 application, and I am trying to limit the length of a database TEXT column up to 500. I have it set as: $table->text('excerpt'); How can I modify this so that it is limited to 500 characters?
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
0
votes
1 answer

Laravel Many to Many relation Cannot add foreign key constraint

I'm creating a ManyToMany relationship with the Laravel framework v.5.7. So I created three migrations. Modules Table Schema::create('modules', function (Blueprint $table) { $table->increments('id'); $table->string('name')->unique(); …
Markus
  • 1,909
  • 4
  • 26
  • 54
0
votes
1 answer

Laravel 5.7 Relationship One To Many Columns

i have team and match table. All match information save on match table with two team, winner team, match time etc. on match table i have three field team_1, team_2, winner_team i want to relation those field with team table Here is my code Team…
0
votes
2 answers

How to add unique key for 2 columns : employee_id and only month and year from the date column

I have a table where I have all the employee details. I want to make employee_id and only month and year from date_of_salary as unique on the table. Only one row should be present for that particular employee ID and month/year. The date_of_salary is…