【发布时间】:2022-07-21 09:22:29
【问题描述】:
我的表格中有独特的图像列。当我尝试用 faker 播种数据时,它失败了。由于数据独特,Laravel 无法播种数据。即使有 2 行也找不到唯一数据:Post::factory(2)->create();
这是我的桌子:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('position');
$table->boolean('status')->default(true);
$table->string('slug')->unique();
$table->foreignId('category_id')->constrained('post_categories')->onDelete('cascade');
$table->boolean('mainpage')->default(false);
$table->string('image')->unique();
$table->string('title')->unique();
$table->text('text');
$table->string('description')->nullable();
$table->string('keywords')->nullable();
$table->timestamps();
});
这是工厂:
private $num = 0;
private $imagePath = 'images/posts';
private $imageWidth = 1280;
private $imageHeight = 720;
public function definition()
{
$this->num++;
Storage::makeDirectory($this->imagePath);
$uniqueWord = $this->faker->unique()->word;
return [
'position' => $this->num,
'status' => $this->faker->boolean,
'slug' => _slugify($uniqueWord),
'category_id' => 1,
'image' => $this->faker->unique()->image(storage_path('app/public/' . $this->imagePath), $this->imageWidth, $this->imageHeight, null, false),
'title' => $uniqueWord,
'text' => $this->faker->paragraph,
];
}
如您所见,我正在使用 faker 上传假图片。但是,当我尝试播种独特的数据时,它会失败。
问题:有没有办法在列唯一的情况下上传假图片?
【问题讨论】:
-
好吧,问题是 Laravel 根本无法上传图片,这就是它无法获得唯一值的原因。但是,为什么 Laravel 不能播种?