【问题标题】:Can't seed unique images with faker in laravel无法在 laravel 中使用 faker 播种独特的图像
【发布时间】: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 不能播种?

标签: php laravel faker


【解决方案1】:

我想我试图在这篇文章中解决这个关于faker生成图像的问题,我希望我可以帮助你 https://stackoverflow.com/a/73059602/16158744

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-03
    • 2023-03-03
    • 2022-07-02
    • 2018-01-26
    • 2017-08-29
    • 2019-05-18
    • 2018-09-10
    • 2015-06-02
    相关资源
    最近更新 更多