I am new to laravel.
I am working on laravel version 6.
I have created migration.
It works nicely the first time, but if i change something in the migration file and then i run php artisan migrate it shows nothing to migrate.
I tried php artisan migrate --path as well but it does not work.
To make it work i have to delete the migration file and create it again.
I don't want to use php artisan migrate:fresh.
what should i do to run only one migrations file which has been changed?
When you run php artisan migrate it'll check migration table if there is no new file in the migration folder it'll show you nothing to migrate.
If you want to rollback last migration.
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
It'll delete the last created table.
The migrate:reset command will roll back all of your application's migrations:
php artisan migrate:reset
The migrate:fresh command will drop all tables.
php artisan migrate:fresh
php artisan migrate:fresh --seed
more info : document
Sadly impossible. The best workaround is to use seeders and use php artisan db:seed after you use php artisan migrate:fresh. Why don't you want to use that?
there is two things to do you that you can use
1. In your database there is a table called migrations. Delete the rows from there which one is you want to migrate. That should be there.
2.create a folder inside of database/migrations/folder. And put the migrations file inside the folder then in your command prompt run this below command:
php artisan migrate:refresh --path=database/migrations/folder
option 2 is better than the option 1. I always use this. So i recommend option 2. This should be work
If you have seeder data then you simply do: php artisan migrate: fresh -- seed . It help to re-migrate your migration with seeder data
You didn't understand well migrations mechanics.
but if i change something in the migration file and then i run php artisan migrate it shows nothing to migrate
You write migration to make some changes in database and run it once. If you want to do next updates to database you need next migration.
During development process you can rerun migrations by php artisan migrate:fresh, but on production make sure your migration makes everything you want.
Laravel stores informations about migrations in database in table 'migrations'. If you want to reset some migrations files you can try deleting or edit some records in that table.
Have you checked that the migration has already run or not in the migration table.
If its run then there will be a row respective to your migration.
If you do changes to an old migration than nothing will be reflected when you run command php artisan migrate, as it has already been migrated.
For every modification on existing (already migrated tables) you will have to make a new migration with modifications only. If you have "users" table migration 2014_10_12_000000_create_users_table like:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
and you need to split "name" column, will have to php artisan make:migration alter_table_users --table="users" and add what you want to change:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'first_name'); // rename column
$table->string('last_name'); // add new column });
Reverse
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('first_name', 'name');
$table->dropColumn('last_name');
});
}
Nou you can use php artisan migrate
Documentation: https://laravel.com/docs/6.x/migrations#modifying-columns
No one replied question "Akshay Rathod" but "Militaru" replied exactly what he need php artisan make:migration alter_table_users --table="users" and copy you new fields inside the up() function as under
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('userimage')->nullable();
$table->string('contact_no')->nullable();
$table->string('website')->nullable();
$table->string('country_id')->nullable();
$table->string('timezone')->nullable();
$table->string('currency')->nullable();
$table->string('communication_email')->default(1);
$table->string('communication_phone')->default(0);
$table->string('allow_marketing')->default(0);
});
}
Related
I have a migration file that I want to run, (only this file not all the migrations file).
I run this command:
php artisan migrate --path=/database/migrations/2019_08_21_225302_delete_encode_crids_ssp.php
In vscode I open the terminal and get this message:
How I can make it to run this specific file, (not want all the files just this file)
Project tree:
this is the code:
public function up()
{
Schema::table('ssp', function (Blueprint $table) {
$table->dropColumn('encode_crids');
});
}
try to do again and get this:
and again:
If you want to re-run the migration with force, use migrate:refresh
php artisan migrate:refresh --path=/database/migrations/2019_08_21_225302_delete_encode_crids_ssp.php
With using refresh() command
With using fresh() command
I created migration on my Laravel project using command php artisan make:migration migration_name and php artisan make:model ModelName -mcr.
When I run php artisan migrate the output is nothing to migrate.
I check my database, there is only migration table which has no row, even user table that comes from Laravel does not created.
This issue occurs on my laptop and PC
This is the environment that I use to run Laravel using XAMPP
Laravel 7.24 and Laravel 5.8.38
Apache/2.4.39 (Win64)
PHP 7.3.7
MariaDB 10.3.16
Composer 1.10.10
This is the migration code
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
//File name = 2020_08_11_064146_create_category_table.php
//File Path = database/migrations
class CreateCategoryTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('category', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('category');
}
}
I already try these but found no luck :
Run composer dump-autoload
Run php artisan migrate:reset -> nothing to rollback
Run php artisan migrate:fresh-> dropped all table successfully, migration table created successfully ,nothing to migrate
Run php artisan migrate --path="/database/migrations/" -> nothing to migrate
Run php artisan migrate:status -> no migrations found
Run php artisan migrate:install -> Migration table create successfully, but did not solve the problem
TLDR :
What I literally did are :
Download Laravel with composer
Edit .env for connection to database using user root
Create migration using php artisan make:migration create_table_category
Run php artisan migrate
Result = Migration table create successfully, nothing to migrate. Database only have table migrations with no rows
EDIT
Migration can be run if I specify the path completely with file name like php artisan migrate --path="database/migrations/2020_08_11_064146_create_category_table.php"
This answer is not the best solution for such situation, but You can try to define migration path hard in AppServiceProvider:
/**
* Register Custom Migration Paths
*/
$this->loadMigrationsFrom([
database_path().DIRECTORY_SEPARATOR.'migrations'
]);/
I stumbled into this post which described that the problems caused by project path that have character hyphens '-'
My project does not have those characters, but it has 'weird' characters and that is opening and closing square bracket '[ ]', so I thought to change it.
My root project directory path is F:\Indra\Kerja\[1] Personal\Personal profile\web so my migration path is F:\Indra\Kerja\[1] Personal\Personal profile\web\database\migrations
Notice there's folder named [1] Personal, that's the culprit
I renamed my folder to Personal and voila the migration works normally.
I was curious so I try different folder name and I get and interesting result:
[asdasd]Personal -> migration doesn't work for some reason
[1 Personal-> migration work
]1Personal-> migration work
][1 Personal-> migration work
[]Personal -> migration work
So I have to change my folder name
Important Note:
My Operating system is Windows 10 Pro Version 1909 Build 18363.1082
I also tried to give hyphens '-' to my project directory like this F:\Indra\Kerja\my-project-test, and expect that the migration won't work, but the migration works without a problem
I am a new learner of Laravel.And I follow the tutorial to create a articles table.Here's part of my code in /database/migrations/2017_02_13_145946_create_article_table.php
public function up()
{
//
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->text('body')->nullable();
$table->integer('user_id');
$table->timestamps();
});
}
When I run php artisan migrate the table was not created.I googled the problem and ran php artisan migrate:reset command to delete all the tables.When I ran php artisan migate command again.It shows
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrated: 2017_02_13_145946_create_article_table
But nothing was created but only updated the record of migrations table.The table user and password_resets was also not created.
Any ideas what I might be doing wrong?
It is because of Table column(values) length is by default 1071 which not accept by mysql
To clear this problem go to mysql and delete all the tables.
Go to your application(project) folder => app => Providers
write this on top of your AppServiceProvider.php file.
//edit:yrs:- Needed for edit
use Illuminate\Support\Facades\Schema;
write this inside the boot method
//edit:yrs:-we can edit column sizefor tables here
Schema::defaultStringLength(150);
then come to console and execute the
*command />php artisan migrate:status and see the run status is y or n if it is n then execute
*command />`php artisan migrate`
*command />`php artisan migrate:status` and then
*command />`php artisan migrate:refresh` and then
*command />`php artisan migrate:status`
that's all your done with createing with tables in mysql
I normally try several things:
In the console do rollbacks till you get 'nothing to rollback':
php artisan migrate:rollback
Then, check your article migration file and look for down function (if you haven't one you need to add it)
public function down()
{
// Drop articles table
Schema::drop('articles');
}
Save your migration article file(I know it sounds dumb, but it happened to me before). I would recommend to change the name of the file also, from:
2017_02_13_145946_create_article_table.php, to:
2017_02_13_145946_create_articles_table.php
Use:
composer dump-autoload
Go to your database(laravel_db in this case) and check if there are any table(Mysql Console):
show databases;
use laravel_database:
show tables;
If there are not any table but migrations, delete it using:
drop table migrations;
Once you have the database empty, run the migrations from console:
php artisan migrate
Finally, if not errors being trowed, check the tables created(Mysql Console):
show tables;
I'm trying to modify a existing migration. Here is my current migration class:
class CreateLogForUserTable extends Migration
{
public function up()
{
Schema::create('log_for_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('table_name');
$table->string('error_message');
$table->unsignedTinyInteger('error_code');
$table->timestamps();
});
}
public function down()
{
Schema::drop('log_for_user');
}
}
I've executed the php artisan migrate command once. Now I need to add ->nullable() method to the error_message column. So I edited my migration, something like this:
.
.
$table->string('error_message')->nullable();
.
.
But when I execute php artisan migrate again, it says:
Nothing to migrate.
How can I apply the new version of the migration?
You should create a new migration using command:
php artisan make:migration update_error_message_in_log_for_user_table
Then, in that created migration class, add this line, using the change method like this:
class UpdateLogForUserTable extends Migration
{
public function up()
{
Schema::table('log_for_user', function (Blueprint $table) {
$table->string('error_message')->nullable()->change();
});
}
public function down()
{
Schema::table('log_for_user', function (Blueprint $table) {
$table->string('error_message')->change();
});
}
}
To make these changes and run the migration, use the command:
php artisan migrate
and to rollback the changes, use the command:
php artisan migrate:rollback
You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last five migrations:
php artisan migrate:rollback --step=5
See more about Modifying columns with Migration
If your app is not in production and you seed your data, the best you can do is to run:
php artisan migrate:refresh --seed
This command will drop all tables and recreate them. Then it will seed the data.
If you will create additional migrations for each change during development, you'll end up with hundreds of migrations classes.
You can use the change method, it allows you to modify some existing column types to a new type or modify the column's attributes.
For example modify a column to be nullable:
Schema::table('log_for_user', function ($table) {
$table->string('error_message')->nullable()->change();
});
But first of all you'll need the doctrine/dbal package
composer require doctrine/dbal
There is one more option. Roll back the migration, edit the file, and run it again.
This is a fairly common thing to do on a local development server while you're working out the bugs in a new piece of code. Using the method from the accepted answer, you might end up with 17 migrations for creating a single table!
php artisan migrate
# realize you've made an error
php artisan migrate:rollback
# edit your migration file
php artisan migrate
The number of steps back to take can be specified on the command line if needed.
# undo the last 3 migrations
php artisan migrate:rollback --step=3
Or you can specify a particular migration that needs undoing.
# undo one specific migration
php artisan migrate:rollback --path=./database/migrations/2014_10_12_100000_create_users_table.php
There are 2 ways to do this:
Run php artisan migrate:refresh. This will rollback all your
migrations and migrate all your migrations. If you run this command,
all the data inserted in your database will be lost.
Run php artisan make:migration enter_your_migration_name_here.
Then insert this in your migration:
$table->string('error_message')->nullable()->change();
Then run php artisan migrate to make your table changes. (Take note that when you do this, you have require composer require doctrine/dbal in your composer)
I use Laravel 5.2 and want to change default user table's columns. I wrote
in the CreateUsersTable
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function ($table) {
$table->string('role');
$table->string('info');
$table->string('age');
$table->string('hobies');
$table->string('job');
});
}
and i run these commands in the git bash but user table didn't change.
php artisan migrate:refresh
php artisan migrate
How can i solve it?
You should run composer dumpauto -o after running php artisan migrate command to register new migrations, so they could be rolled back.
Try it. If it will not work, try to delete all tables, including migrations and run this command:
php artisan migrate:install
create new migration file and do something like this
public function up() {
DB::statement('ALTER TABLE user ADD some_field INT');// your query here
}
php artisan migrate
You only need to change what it inside of Schema::create and delete your Schema::table
Then, you need to implement tour down() mehod and add Schema::drop('users'); in order to drop your table when you run migrate:refresh.
You can read the 'Database migrations' in the laravel documentation for further details.
delete the first statement and leave last one only .
then run php artisan migrate:refresh