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

Laravel 5.2 php artisan migrate:rollback error

I use Laravel 5.2 and I created database tables by running php artisan make:migration create_categories_table --create=categories and php artisan make:migration create_posts_table --create=posts and then I run php artisan migrate, and tables…
minchevz
  • 205
  • 2
  • 15
9
votes
11 answers

PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'

I am using Laravel 5 and getting the following exception: PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge' My database configuration file is:
Hassan Saqib
  • 2,597
  • 7
  • 28
  • 51
8
votes
1 answer

Is Laravel migration ip type ipv6 ready?

I need to save Ip's in the database. I am using laravel but I need to store IPv6 and IPv4 ip's Is the ip type ready for IP? $table->ipAddress('visitor'); Or do I need to use a the normal string type. Thanks
KBeckers
  • 416
  • 4
  • 12
8
votes
1 answer

Creating database view in migration laravel 5.2

I'm trying to create a database view via a migration in Laravel 5.2 as I need to hand a fairly complex query to a view. I have models / tables for leagues, teams, players, and points. Each one has a hasMany / belongsTo relationship with the one…
7
votes
1 answer

Laravel migration boolean field created in Db like tiny integer

I wrote a migration in Laravel: Schema::create('test', function (Blueprint $table) { // $table->increments('id'); $table->string('city','30')->unique(); $table->string('car','30')->unique(); …
Stefano Maglione
  • 3,946
  • 11
  • 49
  • 96
7
votes
6 answers

How to set auto increment into non primary key?

How would I set a database auto increment field that is not a primary key on the creation method? The only way is using a raw query? DB::statement('ALTER TABLE table CHANGE field field INT(10)AUTO_INCREMENT');
Luiz
  • 2,429
  • 8
  • 28
  • 43
7
votes
3 answers

Model not linking to table in laravel php

I have a table named tasks and a model named Task. In my controller method when i run this piece of code
akshay
  • 209
  • 1
  • 4
  • 13
7
votes
1 answer

How to set default DateTime() in Laravel migrations?

I am trying to achieve the functionality illustrated below: $table->dateTime('time')->default(new \DateTime()); This exact code is failing, because [ErrorException] Object of class DateTime could not be converted to string The other…
Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
6
votes
1 answer

Laravel Changing Migration field Float to Double

I want to create a table with a float column. $table->float('TaxRate',5,5)->default(0.00000); But Mysql Taking this as a DOUBLE. How this working ?.
Jishad
  • 2,876
  • 8
  • 33
  • 62
6
votes
2 answers

Laravel/lumen 5.2 generate migration tables from existing database

Is possible to generate migration schema from existing database in lumen/laravel 5.2 ? is there any package? I connect lumen to magento database, now I need to use eloquent. I dont have time to make migration model for every table.
Adnan
  • 7,825
  • 3
  • 23
  • 41
6
votes
3 answers

Foreign key constraint error when refreshing migrations - Laravel

I'm having a problem with migration on my Laravel project. Since i'm fairly new to Laravel I can't figure it out. I want to add a foreign key to an already existing table and this works, but when I refresh my migrations I get this…
Mathijsvdb
  • 75
  • 1
  • 7
5
votes
1 answer

Updating the data of an existing column and copying data with migrations

Is it possible to add a new column, copy some data to this new column and update the data in another column with a Laravel migration? I have a table something like this; id item price 1 ItemOne 100.00 2 ItemTwo 200.00 Now what I need…
5
votes
2 answers

Postgres and Laravel how to change column from type string to integer?

I am trying to change a column from type string to integer on Postgres and Laravel 6.x. I've tried to do this with a migration like so: public function up() { Schema::table('job_listings', function (Blueprint $table) { …
Connor Leech
  • 18,052
  • 30
  • 105
  • 150
5
votes
3 answers

Create a tiny integer column with custom size in Laravel migration

How can I add tiny integer column using laravel migration to MySQL? I thought this code $table->addColumn('tinyInteger', 'birth_day', ['lenght' => 2]); But it creates TINYINT(4) column. I don't know how I can solve this problem. Please don't ask me…
Lukas Pierce
  • 807
  • 3
  • 9
  • 15
5
votes
5 answers

Remove specific migration in laravel

As per laravel doc, To rollback the latest migration operation, you may use the rollback command. This command rolls back the last "batch" of migrations, which may include multiple migration files: php artisan migrate:rollback You may rollback a…
CodeBriefly
  • 1,000
  • 4
  • 16
  • 29
1 2
3
32 33