【发布时间】:2021-02-28 23:02:21
【问题描述】:
我目前正在学习在 laravel 中创建购物车。但是在运行种子命令时,我收到一个名为 Class 'Database\Seeders\App\Models\Product' not found 的错误
首先我创建了名为“Product”的数据库迁移并添加了这些。
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('imagepath');
$table->string('card-title');
$table->text('card-text');
$table->integer('card-price');
});
}
然后我创建了一个名为“ProductTableSeeder”的播种机。 之后,我在 App\Models
中的 Product.php 中的数组中定义了所有这些class Product extends Model
{
protected $fillable = ['imagepath','card-title','card-text','card-price'];
}
然后我在“ProductTableSeeder”中传递数组
public function run()
{
$product= new App\Models\Product ([
'imagepath'=>'assets/img/plants/7.jpg',
'card-title'=>'Books',
'card-text'=>'aaaaaaaa',
'card-price'=>'500',
]);
$product->save();
最后,在“DatabaseSeeder”中,我调用 ProductTableSeeder 以在我运行种子命令后执行
public function run()
{
$this->call(ProductTableSeeder::class);
}
【问题讨论】: