我正在遵循一个教程,我正在尝试让 faker 将数据放入数据库中。我正在使用这个代码。
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Listing>
*/
class ListingFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => $this->faker->sentence(),
// 'tags' => 'Laravel, API, Backend',
// 'company' => $this->faker()->company(),
// 'location' => $this->faker()->city(),
// 'email' => $this->faker()->companyemail(),
// 'website' => $this->faker()->url(),
// 'description' => $this->faker()->paragraph(5),
];
}
}
我只是将其限制在标题中,试图找出我的问题是什么。 这是我遇到的错误。
` INFO Seeding database.
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'tags' doesn't have a default value (Connection: mysql, SQL: insert into `listings` (`title`, `updated_at`, `created_at`) values (Ea a voluptatem cum quidem eos aperiam., 2023-10-23 15:46:39, 2023-10-23 15:46:39))
at vendor\laravel\framework\src\Illuminate\Database\Connection.php:801
797▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
798▕ );
799▕ }
800▕
➜ 801▕ throw new QueryException(
802▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
803▕ );
804▕ }
805▕ }
1 vendor\laravel\framework\src\Illuminate\Database\Connection.php:580
PDOException::("SQLSTATE[HY000]: General error: 1364 Field 'tags' doesn't have a default value")
2 vendor\laravel\framework\src\Illuminate\Database\Connection.php:580
PDOStatement::execute()`
我尝试准确复制代码,但找不到错误。 预先感谢您的帮助。
我只是想在数据库中植入一些虚假数据进行测试
除非
tags
列可为空,否则您需要传递它才能从工厂创建模型。
Listing::factory()->create(['tags' => 'Some, Tags, Here']);