小编San*_*hya的帖子

BadMethodCallException 带有消息“调用未定义的方法 App\Models\Product::factory()”

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

error BadMethodCallException with message 'Call to undefined method App\Models\Product::factory()'. User_id in Product is the foreign key.
Run Code Online (Sandbox Code Playgroud)

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

型号:Product.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

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


}
Run Code Online (Sandbox Code Playgroud)

型号:User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are …
Run Code Online (Sandbox Code Playgroud)

model factories laravel tinker laravel-8

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

factories ×1

laravel ×1

laravel-8 ×1

model ×1

tinker ×1