Factory / Faker在Laravel 5.3 DatabaseSeeder中不起作用

时间:2016-08-24 21:22:07

标签: php database laravel laravel-seeding

我已经将我的代码从Laravel 5.2移到了5.3,一切正常,除非我运行DatabaseSeeder。这在Laravel 5.2中完美运行,现在当我尝试在5.3中运行php artisan db:seed时,当Seeder使用Faker时我得到此错误:

[BadMethodCallException] Call to undefined method Illuminate\Database\Query\Builder::lists()

似乎所有东西都安装得正确。

代码示例:

<?php

use App\Models\Gallery;
use App\Models\GalleryImage;
use Illuminate\Database\Seeder;

class GalleryImageTableSeeder extends Seeder
{

    public function run()
    {
        DB::table('gallery_images')->delete();

        $faker = Faker\Factory::create();
        $gallery = Gallery::lists('id')->All();

        foreach(range(1, 98) as $index) {
            GalleryImage::create([
                'page_id' => null,
                'gallery_id' => $faker->randomElement($gallery),
                'alt' => 'Image description',
                'large' => '201508183828fh5ntu80ub-or.jpg',
                'medium' => '201508183828fh5ntu80ub-lg.jpg',
                'thumbnail' => '201508183828fh5ntu80ub-th.jpg',
                'order' => 1,
                'cover' => false,
                'visibility' => 'visible',
            ]);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

最后我发现它是mysql。需要将list更改为pluck

示例:

$gallery = Gallery::pluck('id')->All();