【问题标题】:Laravel 8 Seeder Factory : Call to a member function count() on nullLaravel 8 Seeder Factory:在 null 上调用成员函数 count()
【发布时间】:2021-05-05 17:58:41
【问题描述】:

当我开始使用 db:seed 为我的数据库播种时,我收到了这个错误:

  Call to a member function count() on null

  at P:\<folderName>\<folderName>\src\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\HasFactory.php:18
     14▕     {
     15▕         $factory = static::newFactory() ?: Factory::factoryForModel(get_called_class());
     16▕
     17▕         return $factory
  ➜  18▕                     ->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : null)
     19▕                     ->state(is_array($parameters[0] ?? null) ? $parameters[0] : ($parameters[1] ?? []));
     20▕     }
     21▕
     22▕     /**

  1   P:\<folderName>\<folderName>\src\database\seeders\UserSeeder.php:23
      App\Models\UserPhoto::factory()

  2   P:\<folderName>\<folderName>\src\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36
      Database\Seeders\UserSeeder::run()

按照Documentation 中的示例,我尝试覆盖默认工厂,但没有帮助。

这是我的模型:

class UserPhoto extends Model
{
    use HasFactory;

    /**
     * Create a new factory instance for the model.
     */
    protected static function newFactory()
    {
        return UserPhotoFactory::new();
    }

}

如果我正常删除-&gt;has(UserPhoto::factory(1))Users 表种子

播种机:

class UserSeeder extends Seeder
{
    public function run()
    {
        $city = City::first();
        User::factory(15)
            ->for($city)
            ->has(UserPhoto::factory(1))
            ->create()
        ;

//        UserPhoto::factory();
    }
}

工厂:

class UserPhotoFactory extends Factory
{
    protected $model = UserPhoto::class;

    public function configure()
    {
        $this->afterCreating(function (UserPhoto $photo) {
            $height = $this->faker->numberBetween(100, 900);
            $width = $this->faker->numberBetween(100, 900);
            $contents = file_get_contents('www.placecage.com/c/'.$height.'/'.$width);
            $path = 'users/images/'.$photo->user_id.'/'.$photo->id.'.jpg';
            Storage::disk('local')->put($path, $contents);
        });
    }

    public function definition()
    {
        return [
//            'created_at' => Carbon::now()
        ];
    }
}

用户模型:

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    protected $hidden = [
        'password',
    ];

    public function city() {
        return $this->belongsTo(City::class);
    }

    public function photos() {
        return $this->hasMany(UserPhoto::class);
    }
}

【问题讨论】:

  • 老实说,这段代码是一团糟。您需要阅读有关工厂的文档。例如,您不需要模型上的工厂方法。此外,一些定义应该从configure 移动到definition

标签: php laravel laravel-8 factories


【解决方案1】:
【解决方案2】:

尝试检查 City 模型中 UserPhoto 的关系名称,我假设您使用的是 userPhoto 所以我将它作为 has(UserPhoto::factory(), 'userPhoto') 中的第二个参数传递了

class UserSeeder extends Seeder
{
    public function run()
    {
        $city = City::first();
        User::factory(15)
            ->for($city)
            ->has(UserPhoto::factory(1), 'userPhoto')
            ->create()
        ;
    }
}

【讨论】:

  • 你为什么说City模特? UserPhotoUser 相关。因此,我尝试将关系名称 'photos' 作为第二个参数传递,但它没有帮助。我还在我的问题中添加了User 模型。
猜你喜欢
  • 2021-03-30
  • 1970-01-01
  • 2021-09-24
  • 2022-01-08
  • 2021-05-04
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
相关资源
最近更新 更多