php artisan migrate not working properly - php

i am new to laravel and i ran the auth file to make a register and login etc...
i have 3 migrations file inside the migrations folder: user table reset_password table and user_profile table: the problem is when i ran: php artisan migrate it only create the user table without the 2 other table so i tried these commands:php artisan migrate:rollback, php artisan migrate:refresh, php artisan config:cache php artisan config cache:clear and then i ran :php artisan migrate and only user table was created... any idea? thank you

I am also facing the same issue, while you are running php artisan migrate, the command starts migrating the table and while migrating users table it is throwing some error like:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key
was too long; max key length is 767 bytes (SQL: alter table
users add unique users_email_unique(email))
so, only user table is migrated and the process stops with the above error. If this is the issue, then you have to solve it and after that delete the users table and again migrate, every thing will be fine.
To fix the above issue, all you have to do is to edit your app/Providers/AppServiceProvider.php file and add to the boot method a default string length like:
use Illuminate\Support\Facades\Schema;
function boot()
{
Schema::defaultStringLength(191);
}
and again migrate, everything will be fine.

I Found the solution for this
First set this in App/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
Then Migrate Cmd And at Last
php artisan migrate:fresh
after that migration works

Related

Laravel Nothing to Migrate

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

Laravel Migration table not created

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;

How can I modify a migration in Laravel?

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)

php artisan migrate:refresh | ERROR [Laravel5]

I use Laravel 5.
when i try to refresh the migration,
INPUT:
php artisan make:migrate
OUTPUT :
[Symfony\Component\Debug\Exception\FatalErrorException]
Class '****' not found
Even i remove that classes also.
Just run
composer dump-autoload
The Class "Todo", which I created record is exist in "Migration" table. Command working when I remove that record.

Laravel's Artisan says nothing to migrate

I installed migrations with php artisan migrate:install then created a migration with the php artisan migrate:make create_teams_table command. Now I try to run them with the following command that I made according to the official documentation:
php artisan migrate --path=app/foo/migrations/2014_01_21_143531_create_teams_table.php
This gives me the following on the console:
Nothing to migrate.
The migrations table in the database is empty and the new table isn't created neither. I don't understand why the documentation says foo in the path. What does foo mean and where does it comes from? First I tought that the path is wrong because of the foo thing and as I know the path is relative to the app folder so I changed it to app/database/migrations but it doesn't work. I also tried a lot of other path combination but none of them worked.
Did I entered the wrong path? In this case shouldn't the console show some other kind of helpfull message? What does foo mean? How can I run my migration?
Try this:
First:
php artisan migrate:reset
Rolled back: 2014_03_28_142140_user_table
Nothing to rollback.
second:
php artisan migrate
Migrated: 2014_03_28_142140_user_table
check the database.
That foo thing is just an example. Laravel will look for migrations to run in app/database/migrations on default. Try removing that --path parameter and see if it works.
You don't need to move the migration file anywhere, just change its filename; for example, increase time integer and then run the migrate command with the path pointing to the migration. e.g: php artisan migrate --path="database/migrations/2019_07_06_145857_create_products_table.php"
What helped me:
php artisan config:cache
php artisan migrate
The path argument is for creating a migration for example:
php artisan migrate:make create_user_table --path=app/database/migrations/user_migrations/
But it is not documented to use while running the migrations, as it was in prior versions of laravel.
Dropping the --path argument should work in your case
need to delete 2014_01_21_143531_create_teams_table of migrations table.
go to database(phpmyadmin)
open your database name.
3.open migrations table.
delete the 2014_01_21_143531_create_teams_table row
For anyone who still cannot migrate their database:
Assume you have a file to migrate abc_migrate.php.
Firstly put your file inside the new folder named abc_folder.
Then, enter this command
php artisan migrate --path=database/migrations/abc_folder/.
You don't have to add file name at last of the directory path.
Done. Hope it helps.
use
php artisan migrate:fresh
it would refresh your database and run your all migrations again
Note:, don't do this in production, it will wipe your all your data. so before running this command do have a backup of your data. in fact, have backup data whenever you are touching database in production.
All you need to do is refresh the migration
php artisan migrate:refresh --path=app/foo/migrations/2014_01_21_143531_create_teams_table.php
At least in Laravel 7, you have to add ".php" at the end of the name of the migration.
Example:
php artisan migrate --path=database/migrations/yyyy_mm_dd_table_name.php
Hope this answer help somebody.
What's happening here is the filename for the migration is cached, and so even though you are changing the migration file, it is not seeing any changes, and so thinks there is nothing to migrate.
There's two ways to fix this:
You can change the file name slightly from from 2014_01_21_143531_create_teams_table.php to 2014_01_21_143532_create_teams_table.php (incrementing the number by 1) and then run php artisan migrate...
or
You can run php artisan optimize:clear to flush the cache and then run php artisan migrate:fresh to do a fresh migration.
Hope this helps!
The problem arises if the migrations table in the database is empty.
So the solution is open up the tinker from the composer
$ php artisan tinker
>>> Schema::drop('users')
>>> Schema::drop('password_resets')
>>> Schema::drop('orders')
>>> exit
php artisan migrate
Here is the result of the above commands executed
nishanth#localhost:~/Desktop/html/hutch$ php artisan migrate
In Connection.php line 647:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users'
alre ady exists (SQL: create table users (id int unsigned not
null auto_incr ement primary key, name varchar(255) not null,
email varchar(255) not n ull, password varchar(255) not null,
remember_token varchar(100) null, created_at timestamp null,
updated_at timestamp null) default character set utf8mb4
collate utf8mb4_unicode_ci)
In Connection.php line 449:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users'
alre ady exists
nishanth#localhost:~/Desktop/html/hutch$ php artisan migrate:rollback
Nothing to rollback.
nishanth#localhost:~/Desktop/html/hutch$ php artisan tinker
Psy Shell v0.8.17 (PHP 7.1.20-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> Schema::drop('users')
=> null
>>> Schema::drop('password_resets')
=> null
>>> Schema::drop('orders')
=> null
>>> exit
Exit: Goodbye.
nishanth#localhost:~/Desktop/html/hutch$ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2018_08_18_071213_create_orders_table
Migrated: 2018_08_18_071213_create_orders_table
nishanth#localhost:~/Desktop/html/hutch$
Also define the method down(), if it doesn't exist.
Otherwise, it'll show
SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'XYZ.ABC' (SQL: drop table ABC)
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('ABC');
}
In my case, i just had to delete a row from my migrations table and then execute
php artisan migrate --path=app/foo/migrations/2014_01_21_143531_create_teams_table.php

Categories