如何使用Laravel的Faker在存储中生成和下载图像?

发布于 2025-02-03 18:09:06 字数 555 浏览 4 评论 0原文

如何使用Laravel的Faker在存储中生成和下载图像?

我希望在运行播种机下载图像并自动保存在存储/类别文件夹中,但是在运行播种机时,将图像全部下载一秒钟,然后消失或删除,因此我不知道发生了什么。

这是类别的

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class CategoryFactory extends Factory
{
    public function definition()
    {
        return [
            'image' => 'categories/' . $this->faker->image(storage_path('app' . DIRECTORY_SEPARATOR . 'public/categories'),640, 480, null, false)
        ];
    }
}

how to generate and download images in storage using laravel's faker?

I want when running seeders to download images and save them automatically in the storage/categories folder, but when running seeder the images are downloaded all for a second and then they disappear or are deleted, so I don't know what is happening.

this is CategoryFactory

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class CategoryFactory extends Factory
{
    public function definition()
    {
        return [
            'image' => 'categories/' . $this->faker->image(storage_path('app' . DIRECTORY_SEPARATOR . 'public/categories'),640, 480, null, false)
        ];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2025-02-10 18:09:06

存储:: makedirectory('categories')如果将驱动程序设置为public,则将在storege/app/code中创建一个目录。因此,使用faker-&gt; image()路径需要为storage_path('app/dummy')时,路径需要使用。

'image' => 'categories/' . $this->faker->image(storage_path('app/categories'),640, 480, null, false)

Storage::makeDirectory('categories') will create a directory at storage/app/categories if the driver is set to public. So when using with faker->image() the path needs to be storage_path('app/dummy').

'image' => 'categories/' . $this->faker->image(storage_path('app/categories'),640, 480, null, false)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文