为什么在迁移错误中调用种子方法?

时间:2018-12-04 13:19:47

标签: laravel-5 migration

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

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

php artisan make:seeder PageContentImagesWithInitData

两个文件都创建好了,我将它们填充为database / migrations / 2018_12_04_120422_create_page_content_images_table.php:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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');
    }
}

和database / seeds / PageContentImagesWithInitData.php:

<?php

use Illuminate\Database\Seeder;

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

但是我还是出错了...

为什么出错以及如何解决?

谢谢!

2 个答案:

答案 0 :(得分:1)

我认为错误在于您的播种机名称空间, 您可以使用--path而不是--class,尝试这种方法

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

答案 1 :(得分:1)

我有类似的问题,但经过一番搜索后,我找不到有效的决定 这两个文件

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

其中包括我以前的播种器类,但不是当前的我无法运行 我手动删除了供应商目录并运行

composer install

并清除缓存

那有帮助! 您可以尝试这种方式!