Laravel 5.3 db:seed command simply doesn't work - php

I do everything by-the-book:
Installed fresh Laravel 5.3.9 app (all my non-fresh apps produce the same error)
run php artisan make:auth
create migrations for a new table
`php artisan make:migration create_quotations_table --create=quotations
Schema::create('quotations', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
// my problem persists even with the below two columns commented out
$table->integer('creator_id')->unsigned()->index('creator_id');
$table->integer('updater_id')->unsigned()->index('updater_id');
$table->softDeletes();
$table->timestamps();
});
Then I run php artisan migrate
Then I define a new seed php artisan make:seeder QuotationsTableSeeder
The complete content of the file, after I add a simple insert:
<?php
use Illuminate\Database\Seeder;
class QuotationsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('quotations')->insert([
'text' => str_random(10),
]);
}
}
Then I run php artisan db:seed
problem
it simply doesn't work. No feedback presented, no errors in log file.
The probem persists in both my local environment (Win7, newest WAMP server)
and my Digital Ocean VPS powered by Ubuntu 16.04.
All the above steps I took in several separate apps - for no result. Also under Laragon 2.0.5 server.
what I have tried
php artisan optimize as suggested here.
composer dump-autoload i php artisan clear-compiled also have brought no results
I also tried to seed just following the official docs example - failed.
I added use DB; to the seed file - still no result.
to do
help!!! How come they don't work?

Are you calling your seeder inside the DatabaseSeeder class? This way:
database/seeds/DatabaseSeeder.php
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$this->call(QuotationTableSeeder::class);
}
}
Or, add the --class option when using the php artisan db:seed command, this way:
php artisan db:seed --class="QuotationTableSeeder"
After creating or removing your seeders, don't forget to run the following command:
composer dump-autoload

NB:
Please use with caution on DEV ENVIRONMENT and/or DISPOSABLE DATABASES ONLY
If anybody else is having issues with migrating AND seeding at the same time, please try
php artisan migrate:fresh --seed
Worked for me..

Related

How to migrate specific migration file in PHP laravel - Nothing to migrate message

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

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 artisan migrate command not running on Homestead

I recently just setup laravel homestead on my development machine and everything has been smooth and I love it. However, I am trying to add some columns to an existing table using migration but when running the php artisan migrate command doesn't complete. It seems to start but never completes even after leaving it for more than 10mins.
Here's my migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddColumnsToTransfersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('transfers', function (Blueprint $table) {
$table->integer('otp')->after('bank_code')->nullable();
$table->boolean('authorized')->after('otp')->nullable()->default('0');
$table->string('recipient_code')->after('authorized')->nullable();
$table->boolean('approval_required')->after('recipient_code')->nullable()->default('0');
$table->unsignedBigInteger('approved_by')->after('approval_required')->nullable();
$table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('transfers', function (Blueprint $table) {
//
});
}
}
And this is the result of the command php artisan migrate
I know db connection is fine as I am able to login and pull data from the database inside the application. I was able to generate the migration file using php artisan make:migration command so I have no idea why is seems to be stuck when I try to run the actual migration.
It seems there was an issue with mysql service. So I stopped the service by running sudo service mysql restart then ran php artisan migrate, and the table was successfully migrated. Problem solved.

Laravel migrate:refresh Doesn't Work After Composer Update

The command in the title return error message below:
Type error: Too few arguments to function
Illuminate\Database\Schema\Builder::create(), 1 passed in
C:\xampp7\htdocs\assurance-web\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php
on line 221 and exactly 2 expected
I installed the framework with the commands:
composer create-project --prefer-dist laravel/laravel assurance-web
version 5.7
Then executed:
php artisan make:migration create_banks_table --create=banks
I was able to run php artisan migrate with no errors. However, then when I run php artisan migrate:refresh I get the error above.
This is "2018_12_04_033726_create_table_banks.php":
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBanksTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('banks', function(Blueprint $table) {
$table->increments('id');
$table->string('bank_name');
$table->string('bank_code');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('banks');
}
}
Maybe your migration table is compromised.
You can use:
php artisan migrate:fresh
instead of "php artisan migrate:refresh" and the migration will see that it will work.
The "php artisan migrate:fresh" command physically deletes all tables instead of rolling back.

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)

Categories