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

Laravel migration uknown database enum requested

Live application have order table. Schema::create('order', function($table){ $table->increments('id'); $table->integer('user_id')->unsigned(); $table->dateTime('date')->nullable(); $table->enum('status',…
Gardelin
  • 950
  • 11
  • 30
3
votes
2 answers

Where are the methods in laravel ColumnDefinition class implemented?

To write migrations in laravel, we have different methods to apply them to our $table columns. One of them, for example, is nullable() which makes that column nullable. I want to know, where do functions like nullable() have been defined. I cannot…
Majid Alaeinia
  • 962
  • 2
  • 11
  • 27
3
votes
1 answer

How to make a migration in laravel module?

In my Laravel project, I use nwidart package for making modules. Now I want to add deleted_at column to an existing model. I should make a new migration in my module. How can I do this? Here is the package documentation:…
Haniye Shadman
  • 357
  • 1
  • 6
  • 19
3
votes
0 answers

Laravel & MongoDB: Cannot drop an existing collection using migrate:fresh

I am unable to drop MongoDB collections by running migrate:fresh. The project is on Laravel 5.7 and we are using jenssegers/laravel-mongodb. When I try to run migrate:fresh, an error comes up saying MongoDB\Driver\Exception\CommandException : a…
3
votes
1 answer

Laravel , Creating Trigger through migration runs successfully, but couldn't find the trigger in mysql

I've created a trigger using migration in laravel and it migrated successfully, but i couldn't find the trigger in database, can someone please help. My Laravel Version is 5.5 I've added the whole migration file below . use…
3
votes
2 answers

Laravel Database Migration for Two Projects using same database

I have two Laravel projects, one for client-facing (let's call this project A) and the other one for admin (let's call this project B). If I update database, such as creating new table or adding extra column on existing table, using Laravel migrate…
hyun
  • 31
  • 2
3
votes
1 answer

laravel - after removing updated_at and created_at columns from database laravel tries to insert into them

I have deleted created_at and updated_at from database but laravel tries to insert into these columns however I have deleted both from database and code in controller. I think something is cached. BTW I don't want to use public $timestamps = false…
codepro
  • 123
  • 1
  • 1
  • 9
3
votes
3 answers

Laravel, SQL varchar

I am Using laravel with MS SQL. When string datatype is defined in the migration (code attahced) laravel create a nvarchar datatype in the Database instead of varchar. How to restrict laravel or SQL Server to create varchar datatype when a datatype…
3
votes
1 answer

Laravel migration - how to change the `id` field to be primary and auto-incrementing later

My table 'sales_order_details' has the id field as integer 'int(10)' with a default value 0. I need to change it to be a primary auto-increment key in migration. I tried in the following way : public function up() { …
Istiaque Ahmed
  • 6,072
  • 24
  • 75
  • 141
3
votes
2 answers

Renaming column with postgreSQL in a migration

I try to rename a column in a migration: \Schema::table('invitations', function (Blueprint $table) { $table->renameColumn('service_id', 'project_id'); }); Running this result in an error: Unknown database type jsonb requested,…
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
3
votes
0 answers

Laravel 5.3 Writing a DB routine/subroutine in a migration file?

In a previous question, I asked about how to write a trigger and gave a decent example of one way to write it in a laravel 5.X migration file. (note: hard/raw coded) Referenced…
Andre F.
  • 1,123
  • 1
  • 10
  • 27
3
votes
3 answers

Laravel 5 - How to seed table automatically after deploying an app?

I have a certain data that must be in table in order for my app to work, otherwise i get an error message. For example if YOU or anyone else pulled my app from github and if you run php artisan migrate and then try to test the app you would get an…
lewis4u
  • 14,256
  • 18
  • 107
  • 148
3
votes
1 answer

Laravel 5.x Database Triggers and possible best practices

This post is to sort-of inform and ask a question. Hello all, I'm developing a large system that puts triggers to good use. We're currently running the server-side on Laravel 5.2 with php 7 using phpmyadmin. Within Laravel there isn't really…
Andre F.
  • 1,123
  • 1
  • 10
  • 27
3
votes
2 answers

Is there a way to keep the data when updating a database table using Laravel 5?

I have a working Laravel app, I needed to update a Model and add a new column to the database. In order to do this I added the following code to the database/migrations/table.php file: Schema::table('client', function (Blueprint $table) { …
DevOti
  • 133
  • 1
  • 1
  • 11
3
votes
2 answers

Should I manipulate data on a Laravel database migration? Or should I do it somewhere else?

I'm planning a major change to my database that will require a set of SQL instructions (and probably also a non-SQL script) to accommodate the old data to the new schema. Now, is it a good idea to manipulate data on a migration script? Or is it just…
Mauro
  • 3,946
  • 2
  • 27
  • 41