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

Laravel foreign key constraint is incorrectly formed I have searched and cannot find the answer

class CreateMediaTable extends Migration { public function up() { Schema::create('media', function (Blueprint $table) { $table->increments('id'); $table->unsignedBigInteger('id_users'); …
0
votes
0 answers

create migration and model together but add prefix to the migration name

I want to create model and migration together like: php artisan make:model Models/Program -m However, I want to name migration as pas_programs and not programs Is there any way?
0
votes
1 answer

Laravel default user migration

I am trying to use Laravel to set up the required database tables. Using the documented: php artisan make:migration create_users_table; php artisan migrate; However, this generates a simple three-column…
0
votes
2 answers

How to fix foreign key error when running migration

I don't know why it's still don't work and show this: Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table db_rocnikovka.books (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter…
0
votes
1 answer

Namespacing laravel/eloquent migration table entries

I'm creating a laravel project that requires multiple databases.These databases are to be populated using laravel migrations. However, these migrations are to have tables that are the same name on different databases, for…
elb98rm
  • 670
  • 6
  • 21
0
votes
1 answer

Specify connection in a laravel migration

I am creating migrations for a Laravel 6 project. It has multiple database connections for data security. I am attempting to use laravel migrations to control the DB and seed things. In order to be clear within models, I have set up two databases…
elb98rm
  • 670
  • 6
  • 21
0
votes
1 answer

Laravel - When using make:migration created tables in database generates empty migration file

I`m running a new installation of Laravel, with clean database and files. I created a table named "frooth" and it has the columns id, title and created_at (id PK, varchar and datetime) When I run "php artisan make:migration frooth" command, the…
0
votes
0 answers

Include migration file in phpcs

I realize this goes against proper coding standards but I don't care. I want my migration files checked by PHPCS. I removed the following line from my phpcs.xml file. */migrations/* I then formatted the following…
Evan Lalo
  • 1,209
  • 1
  • 14
  • 34
0
votes
2 answers

Cannot run Laravel migration on Homestead

I have been trying to run migration since yesterday but database connection is preventing it Note: I can access the database very well from other projects. This only happens with laravel on homestead. Here is the output of the php artisan migrate…
user5911925
0
votes
2 answers

How do I use the store method so that it can save data to my table

I am dealing with form but my store method is not working I do not know why. Please, could you highlight me with some ideas of what wrong with my code I have tried to adjust my table relationship but still is not working Here is my…
Jimmyjbk
  • 351
  • 3
  • 20
0
votes
1 answer

Laravel Migration - Data type also changes when renaming column

I want to rename the column in database using migration, but when i tried it, the datatype also changes. How to rename column without changing the data type. The current name of the column in the database is payment_amount_cash and its type is…
MDB
  • 339
  • 4
  • 19
0
votes
5 answers

Laravel Migration: Base table or view already exists

Background We're using Laravel (5.8) migration for one of our projects from the very beginning. During development we made some migrations. After some time, we found that, some of the migrations are related to setup/configuration. So we moved them…
Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102
0
votes
2 answers

Integrity constraint violation when using a Foreign key for a user role

I'm trying to define a role for my users using a "role_id" foreign key which referes to the "id" of my "roles" table. The migrations worked well but when I'm trying to register I get an error. Migration users class CreateUsersTable extends…
Petoux
  • 108
  • 1
  • 1
  • 11
0
votes
1 answer

Schema::create Vs Sql

I am new with Laravel and started my personal project. Laravel provide migrations for creating schema for our databases. I have difficulties on implementation choice. The ideas I have are: Create database schema using…
0
votes
2 answers

How to filter query in Laravel Factories

I've got a foreign key of a user_id in my Listing table. In my ListingFactory I can put the user_id of a random user in that spot, but my user also has a is_seller bool. How can I make it so the query in my ListingFactory only gives me an array of…
Max
  • 357
  • 1
  • 6
  • 16