为什么在迁移中调用种子程序方法时出错



在控制台中的laravel 5.7应用程序中,我创建了新的表迁移脚本和种子程序:

php artisan make:migration create_page_content_images_table --create="page_content_images"
php artisan make:seeder PageContentImagesWithInitData

这两个文件都创建得很好,我将它们填充到数据库/migrations/2018_12_04_120422_create_page_content_images_table.php:

<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreatePageContentImagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('page_content_images', function (Blueprint $table) {
$table->increments('id');
$table->integer('page_content_id')->unsigned();
$table->foreign('page_content_id')->references('id')->on('page_contents')->onDelete('RESTRICT');
$table->string('filename', 255);
$table->boolean('is_main')->default(false);
$table->boolean('is_video')->default(false);
$table->string('video_type', 10)->nullable();
$table->string('video_ext',  5)->nullable();
$table->smallInteger('video_width')->nullable();
$table->smallInteger('video_height')->nullable();
$table->string('info', 255)->nullable();
$table->timestamp('created_at')->useCurrent();
$table->unique(['page_content_id', 'filename'], 'page_contents_page_content_id_filename_unique');
$table->index(['page_content_id', 'is_main'], 'page_contents_page_content_id_is_main');
$table->index(['page_content_id', 'is_video', 'filename'], 'page_contents_page_content_id_is_video_filename');
$table->index(['created_at'], 'page_content_message_documents_created_at_index');
});
Artisan::call('db:seed', array('--class' => 'PageContentImagesWithInitData'));   //database/seeds/PageContentImagesWithInitData.php
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('page_content_images');
}
}

和数据库/seeds/PageContentImagesWithInitData.php:

<?php
use IlluminateDatabaseSeeder;
class PageContentImagesWithInitData extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{

DB::table('page_content_images')->insert([
'id'                    => 1,
'page_content_id'       => 1, // About
'filename'              => 'your_vote.jpg',
'is_main'               => false,
'is_video'              => false,
'video_type'            => null,
'video_ext'             => null,
'video_width'           => null,
'video_height'          => null,
'info'                  => 'Site slogan image',
]);
DB::table('page_content_images')->insert([
'id'                    => 2,
'page_content_id'       => 1, // About
'filename'              => 'our_boss.jpg',
'is_main'               => false,
'is_video'              => false,
'video_type'            => null,
'video_ext'             => null,
'video_width'           => null,
'video_height'          => null,
'info'                  => 'Our boss photo',
]);
DB::table('page_content_images')->insert([
'id'                    => 3,
'page_content_id'       => 1, // About
'filename'              => 'our_main_manager.jpg',
'is_main'               => false,
'is_video'              => false,
'video_type'            => null,
'video_ext'             => null,
'video_width'           => null,
'video_height'          => null,
'info'                  => 'Our main manager',
]);
DB::table('page_content_images')->insert([
'id'                    => 4,
'page_content_id'       => 2, // Contact Us
'filename'              => 'office_building.jpeg',
'is_main'               => true,
'is_video'              => false,
'video_type'            => null,
'video_ext'             => null,
'video_width'           => null,
'video_height'          => null,
'info'                  => 'Office building',
]);
DB::table('page_content_images')->insert([
'id'                    => 5,
'page_content_id'       => 2, // Contact Us
'filename'              => 'office.jpeg',
'is_main'               => true,
'is_video'              => false,
'video_type'            => null,
'video_ext'             => null,
'video_width'           => null,
'video_height'          => null,
'info'                  => 'Our Office',
]);
}
}

问题是运行迁移命令时出现错误:

ReflectionException  : Class PageContentImagesWithInitData does not exist
at /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/Container.php:779

我检查了一下,没有发现任何拼写错误或大小写问题。。。我在控制台中运行下一个命令:

php artisan config:cache
composer dump-autoload

但我还是犯了错误。。。

为什么会出错以及如何修复?

谢谢!

我遇到了类似的问题,经过一些搜索,我没有找到有效的决定这两个文件

/vendor/composer/autoload_classmap.php
/vendor/composer/autoload_static.php

其中包括我以前的种子程序类,但不包括我未能运行的当前类我手动删除了供应商目录并运行

composer install

并清除缓存

这很有帮助!你可以试试这个方法!

我认为错误在于您的种子名称空间,你可以用--path代替--class,试试这种方法,

Artisan::call('db:seed', array('--path' => 'path/to/my/seed'));

最新更新