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

Problem 'SQLSTATE[42S02]: Base table or view not found

I have a problem with php artisan tinker, can't find the reason why he wants a 'businesses' table but not a 'business' table. Help me fix mistakes, I feel I did a lot of them) My Problem : Illuminate\Database\QueryException with message…
Mineral
  • 39
  • 1
  • 1
  • 7
2
votes
2 answers

Reverse migration / drop foreign key for custom index name laravel migration

So I have this migration public function up() { Schema::table('xxx', function (Blueprint $table) { $table->foreign('aaa','bbb') // handle identifier name too long …
n00b123
  • 43
  • 5
2
votes
1 answer

Laravel 7 timestamp migration always return 0000-00-00 00:00:00 when importing a csv excel file

I search all the related article but no solution. I was setting up a sitemap for Laravel when I realise the timestamp is not ok. So this is bad for SEO. I always have those 0 in the created_at and updated_at field in the MySQL database. I'm not sure…
Danny HC
  • 31
  • 5
2
votes
1 answer

SQLSTATE[HY000]: General error: 1005 Can't create table `Data`.`company_eligibilities` (errno: 150 "Foreign key constraint is incorrectly formed")

public function up() { Schema::create('company_eligibilities', function (Blueprint $table) { $table->id(); $table->string('marks'); $table->integer('company_id')->unsigned(); …
Curious Coder
  • 33
  • 1
  • 6
2
votes
2 answers

SQLSTATE[HY000]: General error: 1005 Can't create table `Projectname`.`users` (errno: 150 "Foreign key constraint is incorrectly formed")

Schema::create('projects', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); Schema::create('users', function (Blueprint $table) { $table->id(); …
iwf
  • 41
  • 1
  • 5
2
votes
3 answers

Foreign key constraint is incorrectly formed (Laravel Migration)

As written in the error logs, maybe the error is caused different forms in the reference table and parent table but I still don't understand cause I'm new in programming here table 1
Febri Tahta
  • 95
  • 1
  • 7
2
votes
4 answers

How to add the tables in mysql to the laravel migrations

I have a ready database created in MySQL. Now I created a new Laravel 6 project. Is it possible to add the tables in MySQL database into the laravel migrations without losing the data? and is there a way to generate a migration from MySQL to…
2
votes
0 answers

Laravel faker - Generate random user_id for the tasks table

for my tasks table I keep getting SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null when i run php artisan migrate --seed Here's my code for TasksFactory.php use App\User; use App\Model\Task; use Faker\Generator…
MelbDev
  • 355
  • 1
  • 5
  • 12
2
votes
1 answer

Laravel - Change default value eloquent

Is possible to modify the default value response from Laravel Eloquent? I have an API and my database has a migration payment. My payment has a column integer status. $table->integer('status')->default(1); I want to know if is possible to change…
Thiago Valente
  • 673
  • 8
  • 25
2
votes
1 answer

Laravel factory user id always null

When trying to get a random user id in a factory, it always returns null despite the DB containing 50 users (created via the User factory). 'user_id' => User::all()->random()->id I tried to display the retrieved resource doing…
Washery
  • 1,037
  • 1
  • 12
  • 23
2
votes
2 answers

How to drop 'morphs' columns in Laravel migration

I am editing a table so that it will use polymorphic relationships: public function up() { Schema::table('locations', function (Blueprint $table) { $table->morphs('location'); }); } But I do not know the best way…
party-ring
  • 1,761
  • 1
  • 16
  • 38
2
votes
2 answers

Laravel migration, fails on hasColumn method

I am using laravel 5.2 version and PHP 7.1. I am trying to migrate the files from my laravel project where I have code to check column existence as shown below - if (!Schema::hasColumn('table1', 'column1')) { // Add column1 } I know the code is…
DIGVJSS
  • 463
  • 1
  • 10
  • 21
2
votes
2 answers

Syntax error, unexpected ',' when trying to seed in Laravel 5.6 using cmd

I'm trying to follow a tutorial on using command prompt in Windows. It's called Laravel Tutorial: Step by Step Guide to Building Your First Laravel Application and I'm stuck in seed : use Illuminate\Database\Seeder; class TestingTableSeeder extends…
Ihsan Dn
  • 31
  • 8
2
votes
2 answers

Database gets stuck in migration with seeder on production with --force in Laravel 5

Database gets stuck in migration with seeder on production with --force in Laravel. Same effect I have on Laravel Homestead and EC2 AWS running Amazone linux. No messages in laravel.log. It just never ends. If I halt it with +, I see the…
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
2
votes
1 answer

Laravel, copy tables from one database to other

I'm working on a SaaS project. With different client requests for more fields or less etc, i believe it would be helpful instead of just keep growing on already created tables, divide the tables that could indeed grow into separate databases per…
Sérgio Reis
  • 2,483
  • 2
  • 19
  • 32