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

Databse migration throws error while seeding two timestamp columns laravel 5

I extended the default migration to include some additional table fields for my user table. I wanted to have created_at and updated_at fields with timestamps as values. Here is my code
echoashu
  • 912
  • 3
  • 14
  • 31
2
votes
1 answer

Is there a way to create a citext field using Laravel 5 Migrations?

Specifically, since MySQL doesn't have that field type, raw DB queries seem like a bad idea. Since pgsql lacks a case insensitive text field by default, we had to use this extension (which works perfectly, of course), but now are faced with a…
CJ Thompson
  • 2,729
  • 2
  • 22
  • 26
2
votes
2 answers

Laravel migrations not working

I can't make it work to use migrations on my laravel installation (Windows 8.1 host). I make 2 migrations via: php artisan migrate:make create_language_table The file was created under app/database/migrations. I use the Schema Creator to create a…
G-Wak
  • 53
  • 3
  • 12
2
votes
2 answers

Artisan migrate fails in development environment because of missing environment variable in production config

I have two database configs, one for production and one for development: // app/config/database.php 'connections' => array( 'mysql' => array( 'driver' => 'mysql', 'host' => $_SERVER['RDS_HOSTNAME'], 'database' =>…
John Dorean
  • 3,744
  • 9
  • 51
  • 82
2
votes
2 answers

Is it necessary to write down() for every up() in migration in laravel?

class UpdateSessions extends Migration { public function up() { Schema::table('sessions', function($table){ $table->string('comCode')->nullable()->change(); }); } public function down() …
1
vote
2 answers

How to run migration for tables inside Modules in laravel

I am working on Laravel^9.19" project with php^8.1, I created a Modules folder and then I created a migration files inside, my structure is like this…
1
vote
1 answer

Proper way to access migrations files building custom refresh/seed in Laravel

Looking for a smarter way to do this. I have 4 migrations files (out of MANY dozen) that are related. I also have a seeder for these. Doing php artisan migrate:fresh --seed takes a good 30-45 seconds each time (there are also a few dozen seeders)…
Phil Tune
  • 3,154
  • 3
  • 24
  • 46
1
vote
1 answer

How to make bit column like in SQL Server using Laravel Database Migration

So I'm creating a new table, replicating an existing table, and one of them has a column of type bit, like this: I already understand the concept of migration and have made it often, but to create this column of bit type via migration, what type of…
1
vote
3 answers

Laravel Artisan Migrate Drop Column using Sqlite throws exception

I'm trying to set up some unit tests in a legacy Laravel project. This project has a lot of migration scripts creating and updating the database. This migrations work fine on a MySQL database, however I don't want to persist a test database so I'm…
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
1
vote
3 answers

Do I need to create DB migration, if I have a designed DB in MySQL?

I designed a database in MYSQL. Now I want to connect that with my Laravel application. But after researching, I understand that I need to migrate the database into Laravel by creating migration tables. But doing the same thing that I did in MySQL…
Siddikur
  • 31
  • 6
1
vote
1 answer

Laravel Migration Error: SQLSTATE[42000]: Syntax error or access violation: 1091 with mysql

I am learning migration in Laravel. I have created a migration file and migrations seems to work fine from command line. But inside my mysql file the username column is not created after migration. >php artisan make:migration…
Gabriel Rogath
  • 710
  • 2
  • 8
  • 23
1
vote
1 answer

How to use variables instead of column names for running new Migrations directly in the Controller

I'm using Laravel 8 and I wanted to run this code which insert two new columns, if the attribute name does not exist in the returned results of product_attr_info_correction column names. $allAttrs = Attribute::all(); $columns =…
1
vote
2 answers

How to seed related data during migration in Laravel?

I have a table clients and a table contracts. In my Client model I have public function contract() { return $this->hasOne( 'App\Models\Contract', 'client_id', 'id' ); } I don't want to…
PeterPan
  • 195
  • 2
  • 16
1
vote
1 answer

Laravel inserting data in SQL not possible cause of wrong use of functions

I was trying to calculate the time difference between check-in and checkout, but somehow it's not working. I'm pretty sure I'm doing something wrong, but I can't wrap my head around it. SQLSTATE[HY000]: General error: 1364 Field 'hours' doesn't…
1
vote
1 answer

How to migrate Laravel database on Docker onto Google Cloud

My Laravel app is running on docker using Linux commands on a Windows Machine. This means instead of using 'php artisan' commands, I use 'sail artisan' commands. I am able to migrate locally onto the SQL server on docker (sail artisan migrate) and…