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

Laravel - Changed Migration file name/class, migrate:reset still looks for old class

I changed my migration file name, updated the class name, ran 'composer dump-autoload' and then ran 'php artisan migrate:reset'. When I run that I get an error: [Symfony\Component\Debug\Exception\FatalErrorException] Class 'OldClassName' not…
Logik22
  • 51
  • 1
  • 5
3
votes
2 answers

Laravel looks for other migrations

I have two Laravel projects. They are in different folders. It is okay until then. Currently my projects use Laravel 5. The last one. I have one project I tried to migrate. But, I do not have any idea why it is looking for migration files of…
Seiji Manoan
  • 671
  • 6
  • 12
2
votes
1 answer

Why creating table in migration error with created_at table?

Uploading laravel 9 project on remote server with ubuntu 18.04 I got error migrating: 2023_01_18_053125_create_user_meetings_table ............................................................................................ 1ms…
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
2
votes
0 answers

Laravel 9 Migration classname is not being applied from stub

I am trying to create a model along with migration as follows: php artisan make:model Candidate -m The output is: Model created successfully. Created Migration: 2022_08_06_165641_create_candidates_table Now on the migration file:
2
votes
2 answers

Creating a nickname from other columns by migration, laravel 8

I have a table with users, which includes fields for "first name" and "last name" and I need to create a "nick" field during migration and fill them in by creating a string of lowercase letters from the firstname and lastname without…
2
votes
3 answers

How to change primary key in laravel migration

I'm trying to change the primary key on a table so I can use an other column as foreign key too. Here's the migration for the creation public function up() { Schema::create('blacklists', function (Blueprint $table) { $table->id(); …
skytorner
  • 405
  • 2
  • 8
  • 19
2
votes
1 answer

How to use foreignUuid in laravel 9

so im trying to use UUID instead of id in my table i have two tables, in both, I am using UUID to generate an id. after that, I am trying to use one id as a foreign in the second table. Im using laravel 9 here my first table and here is my second…
2
votes
1 answer

Lumen migration - Foreign Id with a table name that isn't the plural of the local key

How can I create a Lumen migration with a column that references a table that has an unrelated name to the column name? Example: The following would throw an error that user_destinations can't be…
njk18
  • 153
  • 7
2
votes
2 answers

Laravel: Unknown column type "timestamp" requested

I am trying to perform a migration and I am getting the following problem. Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known…
2
votes
4 answers

Using Laravel's foreignIdFor method and create a composite unique key

Following the syntax below, I can easily create a composite unique key based on the fields name and bakery_id: Schema::create('product_categories', function (Blueprint $table) { $table->id(); $table->foreignId('bakery_id')->constrained(); …
nekiala
  • 450
  • 9
  • 21
2
votes
1 answer

Run Laravel migrations on just one server (like ->onOneServer() for scheduled cronjobs)

I'm running a Laravel app on multiple servers at the same time. It's running in a Docker image. Each time I deploy some code changes, Docker image is restarted and then it runs after_deployment.sh script which contains php artisan migrate --force.…
Boring person
  • 443
  • 1
  • 5
  • 12
2
votes
1 answer

Laravel migration error: Base table or view not found

I got the following error. How can I fix it? Base table or view not found: 1146 Table 'account_infos' Migration public function up() { Schema::create('account_info', function (Blueprint $table) { $table->id(); …
2
votes
1 answer

Cross-database foreign key issue when database contains a dot(.) value

I am facing an issue to assign across cross-databases relationship in laravel migration because my database name contains a dot(.) value, My Database name is = "demo.local.com" Schema::table('user_details', function(Blueprint $table) { …
Mdr Kuchhadiya
  • 431
  • 4
  • 12
2
votes
2 answers

Deleted migrations files still migrating

I deleted some of the migration files in my laravel app but after I run php artisan migrate command it still migrating the deleted migration files. I already tried composer dump-autoload and clearing caches still the same. What am I…
LAZYASSKID
  • 70
  • 8