BadMethodCallException带有消息“调用未定义的方法AppModelsProduct::factory()”

在 Laravel 8 中,我可以使用工厂为用户生成虚拟数据,但我无法为产品生成数据。我收到以下错误:

error BadMethodCallException with message 'Call to undefined method AppModelsProduct::factory()'. User_id in Product is the foreign key.

我无法确定出了什么问题。

型号:Product.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Product extends Model
{
    //use HasFactory;
    protected $fillable = ['title', 'type', 'firstname', 'surname', 'price', 'papl'];
    
    public function user(){
        return $this->belongsTo(User::class);
    }


}

型号:User.php

<?php

namespace AppModels;

use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function products(){
        return $this->hasMany(Product::class);
    }


}

工厂:ProductFactory.php

<?php

namespace DatabaseFactories;

use AppModelsProduct;
use IlluminateDatabaseEloquentFactoriesFactory;

class ProductFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Product::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'user_id' => factory(User::class),
            'type'=> 'cd',
            'title'=> $this->faker->sentence,
            'firstname'=> $this->faker->username,
            'surname'=> $this->faker->surname,
            'price'=> $this->faker->number,
            'papl'=> $this->faker->number,
        ];
    }
}

用户工厂.php

<?php

namespace DatabaseFactories;

use AppModelsUser;
use IlluminateDatabaseEloquentFactoriesFactory;
use IlluminateSupportStr;

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }
}

有人可以帮我吗?

回答

在您的模型中,您已注释“使用 HasFactory”,因此它永远不会检测到此功能。如果您取消注释并保存它,它应该可以正常工作。


以上是BadMethodCallException带有消息“调用未定义的方法AppModelsProduct::factory()”的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>