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

Can't add foreign key constraint to pivot table

Background: I am building an application with Laravel for coaches to record their sport team's matches during a season. Currently, I am trying to create a pivot table, to map a Team to a Round (match). Relationship overview: A User has one Team,…
AshMenhennett
  • 1,095
  • 4
  • 14
  • 25
0
votes
2 answers

How would I override Laravel 5.3 sql grammar

How would I set Laravel 5.3 to use VARCHAR over NVARCHAR for migrations on mssql? Is there even a way to do this? This question also applies to SMALLDATETIME over DATETIME.
Jason Spick
  • 6,028
  • 13
  • 40
  • 59
0
votes
2 answers

artisan migrate:rollback error in laravel 5.3

I'm trying to build a simple migration table and then trying to add a column in the table so following is my migration file: class AddFlagToEmiTable extends Migration { /** * Run the migrations. * * @return void */ public…
Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
0
votes
2 answers

Laravel phpunit tests will not migrate properly

I have 2 tables, badges and counselors. All of my tests were green. I added a third table, a pivot table, named badge_counselor. Here is the migration: use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use…
Luke Sweeney
  • 905
  • 1
  • 7
  • 17
0
votes
1 answer

Laravel 5.3 Passport migrations & modifying the default users table

I've just upgraded from laravel 5.2.45 to 5.3.15 and, like many others out there, have some questions about how to implement passport and the default tables that come with the command: php artisan make:auth Situation 1: I have set up my…
0
votes
0 answers

Laravel 5 error when using whereIn

I migrated my laravel app from 4 to 5 and i am having an issue with this query: $ids = AssetCustomTag::whereIn('custom_tag_id', $custom)->lists('asset_id')->all(); where $custom is an array of ids. The error thrown from laravel is this…
Leon
  • 1,262
  • 3
  • 20
  • 41
0
votes
1 answer

Laravel 4.2 Migrations issue

Can someone help me with this error? Got it during Laravel 4.2 Migrations {"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Uncaught TypeError: Argument 1 passed to Illuminate\Exception\WhoopsDisplayer::display()…
0
votes
2 answers

Laravel 5: Migration-Route - Artisan::call('migrate', array('--force' => true)) stops

I have an migration-route in my application, the current output is: init migrate:install...done migrate:install init with tables migrations... Now it stops. But the output should continue. Heres the route: Route::get('/migrate', function () { …
lippoliv
  • 727
  • 2
  • 11
  • 28
0
votes
1 answer

How to create a self-reference relationship (foreign key) in a Laravel migration?

I am starting with a self-learning project built on top of Laravel 5.2 and I have found my first issue: self-reference in a migration. This is how the file 2016_08_02_024942_create_navigation_table.php looks like (I have remove the comments for not…
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
0
votes
1 answer

How to set the default value of an integer field to it's possible maximum?

Is there any way to set a maximum value as a default one for an integer field? In other words, I'm looking for a simple and elegant way to replace the hardcoded 999 value from the code below. Schema::create('files', function(Blueprint $table) { …
Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
0
votes
0 answers

Laravel foreign key nullable can not be null on UPDATE

I create a table- GAllERY_ID on POST. That is a foreign key nullable, and null as default. When I create a POST, if I don't set the GALLERY_ID, it will be null. But if I set the GALLERY_ID and reference on a GAllery, after if I try to make this NULL…
murilo
  • 41
  • 5
0
votes
2 answers

Using Laravel migration to rename existing column and then run php artisan migrate:refresh successfully

I've successfully renamed an existing column previously created in a different migration with the following line of code: $table->renameColumn('custom_paper_note', 'custom_primary_paper_note'); However, now when I run php artisan migrate:refresh I…
Bryan Miller
  • 3,262
  • 4
  • 27
  • 51
0
votes
1 answer

how to have html codes in mysql or json in laravel using seeder

I'm trying to build a website builder, where I want to store HTML structure of the plugins/portlets manually, where user can have the structure and whatever editable data they are inserting, gets stored into database using serialize in the…
Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
0
votes
2 answers

Laravel 5.2: Update .env file with user input & run migrations through code

I'm creating an app and I am planning to give users a one click installation feature. The idea is to show a form when the application is first launched, sort of Installation / Configuration screen where user will type Database details (hostname, db…
Jazzbot
  • 385
  • 1
  • 4
  • 18
0
votes
1 answer

Git hooks to run Laravel migrations automatically

I am about to muddle thru creating 2 hooks, but I just wanted to check if someone has these hooks already, or perhaps a better idea. pre-push: use git diff --stat --cached origin/master to see if "migrations" are being pushed. If so, then backup…
Tarek Adam
  • 3,387
  • 3
  • 27
  • 52