Laravel 8 的问题:在 null 上调用成员函数 count()

Posted

技术标签:

【中文标题】Laravel 8 的问题:在 null 上调用成员函数 count()【英文标题】:Problem with Laravel 8: Call to a member function count() on null 【发布时间】:2021-03-30 04:38:16 【问题描述】:

我是 Laravel 的新手,当我尝试使用工厂在数据库中的表中生成一些信息时出现错误。

在 null "处调用成员函数 count() 供应商/laravel/framework/src/Illuminate/Database/Eloquent/Factories/HasFactory.php:17。

也许有人有同样的问题?如果有人可以提供帮助,我将不胜感激。以下是某些元素的代码:

播种机

class UsersTableSeeder extends Seeder

    public function run()
    
        Users::factory()->count(30)->create();
    

工厂

class UploadInfoFactory extends Factory

    protected $model = Users::class;

    public function definition()
    
        return [
            'Name' => $this->faker->name,
            'Birthday' => $this->faker->date('d-m-Y'),
            'Phone number' => $this->faker->phoneNumber,
            'Phone balance' => $this->faker->numberBetween(-50,150),
        ];
    

数据库播种器

class DatabaseSeeder extends Seeder

    public function run()
    
        $this->call(UsersTableSeeder::class);
    

迁移

class CreateInfoUsers extends Migration

    public function up()
    
        Schema::create('info_users', function (Blueprint $table) 
            $table->integerIncrements('id');
            $table->string('name',100);
            $table->date('Birthday');
            $table->string('Phone number',100);
            $table->string('Phone balance',100);
        );
    
  

我们输入php artisan db时在bash中弹出的错误码:seed:

Call to a member function count() on null at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/HasFactory.php:17
     13▕     public static function factory(...$parameters): Factory 
     14▕         $factory = static::newFactory() ?: Factory::factoryForModel(get_called_class());
     15▕ 
     16▕         return $factory
  ➜  17▕                     ->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : null)
     18▕                     ->state(is_array($parameters[0] ?? null) ? $parameters[0] : ($parameters[1] ?? []));
     19▕     
     20▕ 
     21▕     /**

【问题讨论】:

您是否设置了Users 模型的配置以知道使用此UploadInfoFactory?至于这个自动使用工厂,工厂必须有一个特定的名称 【参考方案1】:

如果您希望模型自动使用工厂,则必须以特定方式命名工厂,否则您必须定义一种方法来解析要用于模型的特定工厂。

将您的工厂重命名为UsersFactory,它将自动用作Users 模型。

不过,我建议将 Users 重命名为 User,因为惯例是模型使用单数,表格使用复数。如果您更改型号名称,则需要将工厂更改为 UserFactory 以匹配。

HasFactory trait 的 factory 方法将使用约定来确定模型的正确工厂。具体来说,该方法将在 Database\Factories 命名空间中查找类名称与模型名称匹配的工厂,并且后缀为Factory。如果这些约定不适用于您的特定应用程序或工厂,您可以覆盖模型上的newFactory 方法以直接返回模型对应工厂的实例"

Laravel 8.x Docs - Database Testing - Creating Models Using Factories - Connecting Factories and Models

【讨论】:

感谢您的帮助,我会这样做的,我也会阅读这个 Laravel 文档,希望对我有帮助【参考方案2】:

这可能对其他人有所帮助,因为我的问题有所不同。在为数据库播种时,Laravel 打印出与 @Yurii 遇到的相同的错误。

Seeding: Database\Seeders\ProfileSeeder

   Error 

  Call to a member function count() on null

  at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/HasFactory.php:18

确实,我的 artisan 控制台未能为我的配置文件模型创建工厂

$ php artisan tinker
>>> use Illuminate\Database\Eloquent\Factories\Factory;
>>> Factory::factoryForModel(Profile::class);
=> null

经过几分钟的调查,我发现我忘记在configure方法中返回工厂

class ProfileFactory extends Factory

    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Profile::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    
        return [
            // some stuff
        ];
    

    public function configure()
    

        // it miss the return here !!
        $this->afterCreating(function (Profile $profile) 
            // do stuff with $profile
        );
    

所以,当 Factory 被实例化时,configure 方法被调用但没有返回 Factory!放置退货解决了问题。

【讨论】:

以上是关于Laravel 8 的问题:在 null 上调用成员函数 count()的主要内容,如果未能解决你的问题,请参考以下文章

laravel 8 中的文件上传 - 在 null 上调用成员函数 getClientOriginalName() 时出错

调用字符串上的成员函数 addEagerConstraints() - Laravel 8

在 null sanctun laravel mongodb 上调用成员函数 prepare()

Laravel 8:调用未定义的函数工厂()

在laravel5.8中集成swoole组件

在 PHPUnit 中调用路由时如何在 Laravel 8 中模拟 Eloquent 模型