在 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)