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
1
vote
1 answer

Laravel - Getting "1067 Invalid Default Value" for unsignedInteger on migration

I'm trying to run a migration on Laravel for creating a new table, but I'm getting this error: Syntax error or access violation: 1067 Invalid default value for 'tracked_product_id' I understand there is an issue with the first unsignedInteger line -…
RyanDawkes
  • 47
  • 5
1
vote
1 answer

Laravel how to automate drop tables then import sql file then apply migration?

Because of different namming conventions than Laravel in an other database I need to automate this process in Laravel : Drop all tables import sql dump file replay my migration which rename columns, FKs etc... What could I do to achieve this ? php…
Eloise85
  • 648
  • 1
  • 6
  • 26
1
vote
1 answer

Laravel - SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value

I'm creating a migration with 2 timestamps, but for some reason I can't put 2 timestamps without a default/nullable value. My code: Schema::create('lessons', function (Blueprint $table) { $table->id(); …
Thiago Valente
  • 673
  • 8
  • 25
1
vote
2 answers

how can i create a php file that runs php artisan migrate command?

I want to create a php file in public directory that runs php artisan migrate:fresh (--seed) command in server. when I upload my project to FTP server I want to open that link (ex: www.project.com/migration.php) and that file should run migration…
jigsaw075
  • 155
  • 2
  • 16
1
vote
1 answer

Laravel Migration Nullable Based on Another Column

I'm writing a DB migration using Laravel and I'm wondering if I can set a column as not nullable based on the value of another column. For example: If User.owns_cat === true Then User.cat_name->nullable(false) I know I can handle this via…
Josh
  • 714
  • 2
  • 8
  • 20
1
vote
0 answers

Laravel Postgres multischema DB migrations

I am using Postgres and Laravel 5.8. My database has multiple schemas. Each schema has a patients table. I am creating authentication for the patients in each schema by adding a password column in the existing patient's table. The migration loops…
1
vote
2 answers

Why can't I properly create a table with a foreign key?

I want to create 2 tables: A table called roles And a table called users In users I want to have a column called role_id and it should be a reference to roles table. This is how roles table should look like: So here is a migration for user…
Tyoma Inagamov
  • 125
  • 4
  • 13
1
vote
1 answer

Laravel create table using migration error

I want to create a tbl_teacher_students table using migration.. My command is php artisan make:migration create_tbl_teacher_students_table. I also tried adding --create=tbl_teacher_students but both gives me this error: PHP Fatal error: Cannot…
stackved12
  • 111
  • 1
  • 7
1
vote
1 answer

Adding column using make migration command error

I'm trying to add an icon_path column to an existing table called tbl_device by using php artisan make:migration add_icon_path_to_tbl_device_table --table=tbl_device and after running php artisan migrate, it gives me this error. PHP Fatal error: …
stackved12
  • 111
  • 1
  • 7
1
vote
2 answers

Wrong migration called

I have 2 migrations, the first one has the following name 2019_11_06_171637_create_settings_table.php and structure: class CreateSettingsTable extends Migration { public function up() { Schema::create('settings', function (Blueprint $table)…
hehoboy
  • 75
  • 1
  • 7
1
vote
0 answers

Fix Laravel migrations with SQLite issues on existing project

hope this is not a duplicate question but couldn't find the solution, even though I ran into this issue more than once now so decided to throw it out here. There's a couple of issues I sometimes run in to when it comes to Laravel's DB migrations and…
Timm49
  • 31
  • 2
1
vote
1 answer

Issues when running php artisan migrate tables from laravel app to localhost database

The following: Is the error I would receive (Mind you if it's not the users table, it's the failed_jobs or reset_password table. The majority of the issues comes from the laravel auto-generated tables. And yes I did try PHP artisan migrate: reset,…
Gambit
  • 13
  • 4
1
vote
1 answer

How to clone Laravel migration fields into another migration?

I have this Laravel migration structure: class CreateWarehouseProductTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('warehouse_products', function…
netdjw
  • 5,419
  • 21
  • 88
  • 162
1
vote
1 answer

Cannot add foreign key constrain on delete cascade

I am trying to create a cascade on two migrations: Schema::create('product_product_attribute', function ($table) { $table->bigIncrements('id'); $table->bigInteger('product_id')->unsigned(); …
SPQRInc
  • 162
  • 4
  • 23
  • 64
1
vote
1 answer

Unable to run migration via artisan command

I have a Laravel 7 project and pushed it to a live server for production but I can't run migration successfully. I always get an error 'access denied' error. I can confirm that the command sees the .env file and the connection details are all…
shaNnex
  • 1,043
  • 2
  • 19
  • 34