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

Laravel Dusk: Migrate and Seed Testing DB Once

Is it possible to run migration and seeding once and don't refresh the testing db between the test methods? I have couple of testing functions that depend on each other and I don't want to migrate and seed the database before and after each test in…
Learner
  • 611
  • 3
  • 12
  • 28
1
vote
2 answers

Change date format in Laravel migration

I want to change the date format from 1990-01-30 to 30/01/1990 straight from my migration. I get the following error when I try to migrate with seeding from factory. Invalid datetime format: 1292 Incorrect date value: '30/01/1990' for column…
justice
  • 75
  • 1
  • 2
  • 11
1
vote
0 answers

Laravel Testing with Modules Test setup Migrations not run?

Laravel v5.5.48 Phpunit 6.5.14 Was able to setup the test to run from the real mysql database, but of course, this is not desirable. So, changed phpunit.xml to use sqlite, but now I get "No such table" errors. Digged for hours, but cannot get…
Eric Day
  • 142
  • 1
  • 10
1
vote
1 answer

Laravel - migration, table structure modification - correct way

My present table is Schema::create('students', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('first_name', 255); $table->string('last_name', 255); $table->enum('gender', ['m', 'f']); …
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
1
vote
1 answer

How to handle existing records in migration rollback with unique in Laravel?

I have lots of migrations and usually use 'php artisan migrate:refresh' and everything is fine. For the first time I get an error while rollbacking because of a duplicate entry for a unique column: SQLSTATE[23000]: Integrity constraint violation:…
tverdo
  • 122
  • 10
1
vote
2 answers

Cannot delete or update a parent row: Laravel

I want to delete a record in category table wherein it will also delete the subcategory record which has a the foreign key of category. How can I do this? Also, an explanation would help as why it has happened. Thank you! Controller public function…
kwestionable
  • 496
  • 2
  • 8
  • 23
1
vote
1 answer

Error: "SQLSTATE[42000]: Syntax error or access violation: 1064

I got a Laravel MySQL migration problem. Migration: public function up() { Schema::create('capsule_packages', function (Blueprint $table) { $table->increments('id'); $table->string('name'); …
dlfjj
  • 349
  • 1
  • 8
  • 26
1
vote
1 answer

what is artisan migrate:install use for?

It is not mentioned in the docs and in the CLI help, it says - Creates the Migration Repository. What does that mean? When I run thus command it says - database laravel not found.
R. Gurung
  • 1,356
  • 1
  • 14
  • 34
1
vote
1 answer

Laravel General error: 1005 Can't create table

I get this error when I do php artisan migrate. Is there something wrong in my migration files? Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table test.blog_posts (errno: 150 "Foreign key constraint is…
1
vote
1 answer

How to add new column before first column in laravel?

I need to add a column at first in Sql using laravel migration. i can add new column at any place using after: Schema::table('tasks', function (Blueprint $table) { if (Schema::hasColumn('tasks', 'assigned_to_id') == false) { …
Neha
  • 2,136
  • 5
  • 21
  • 50
1
vote
2 answers

Laravel Seeder class not found when I call it via DatabaseSeeder

I made base upon documentation a Database migration named class GridWithRoverSeeder in ./database/seeds/GridWithRoverSeeder.php: use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; class GridWithRoverSeeder extends Seeder { /** …
Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
1
vote
2 answers

Laravel Seeder not found

I'm running a few seeders during a migration after creating a table. Here's my migration file create_institutions_table use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use…
solidau
  • 4,021
  • 3
  • 24
  • 45
1
vote
3 answers

Current model is incompatible with old migrations

I have following sitation (I will describe it as history line): I setup project witch User model (and users table) with migration file A After some time i add user_modules table many-to-many and I was force to initialize this array during schama…
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
1
vote
1 answer

How to insert data in the enum field?

I've an enum field in my migration. The code is here: Schema::create('clients', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('preview_img'); $table->enum('platform',…
Aleksej_Shherbak
  • 2,757
  • 5
  • 34
  • 71
1
vote
1 answer

Migrate and initialize database not working Laravel

I have a Laravel project new. I want to migrate the database and initialize the database. so inside new project I have vendor/bin(folders)/phinx.php(file) I want to migrate the database and initialize tables I tried: vendor/bin/phinx migrate(not…
traker
  • 101
  • 1
  • 1
  • 9