英文:
Seed fake images in laravel
问题
我试图生成一个虚假的图像来填充种子数据。
我使用以下代码创建了一个种子:
$this->faker->image(storage_path('app/public/products'), 500, 500)
当我尝试通过 Laravel Nova 访问它们以查看它们时,我得到这种类型的URL:http://localhost/storage/var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png
在数据库中,它保存为:/var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png
我做错了什么,有什么想法吗?
英文:
I'm trying to generate a fake image to populate seeder data.
I created a seeder with this:
$this->faker->image(storage_path('app/public/products'), 500, 500)
When I try to access them through Laravel Nova to see them, I get this kind of URL: http://localhost/storage/var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png
In the DB it's saved as: /var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png
Any idea what I've done wrong?
答案1
得分: 1
请查看 image()
的方法签名:
function image(
?string $dir = null,
int $width = 640,
int $height = 480,
?string $category = null,
bool $fullPath = true,
bool $randomize = true,
?string $word = null,
bool $gray = false,
string $format = 'png'
)
将 $fullPath
设置为 false
将解决您的问题:
$this->faker->image(storage_path('app/public/products'), 500, 500, null, false)
英文:
See the method signature for image()
:
function image(
?string $dir = null,
int $width = 640,
int $height = 480,
?string $category = null,
bool $fullPath = true,
bool $randomize = true,
?string $word = null,
bool $gray = false,
string $format = 'png'
)
Setting $fullPath
to false
will fix your issue:
$this->faker->image(storage_path('app/public/products'), 500, 500, null, false)
答案2
得分: 0
尝试这个:
$this->faker->image('app/public/products', 400, 300, null, false)
如果你有一个视图,那么:
<img src="/my/path/{{ $item->image }}" >
英文:
Try this:
$this->faker->image('app/public/products',400,300, null, false)
if you have a view then:
<img src="/my/path/{{ $item->image}}" >
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论