使用 factory faker 创建的图像存储在 storage/public/images 文件夹中时会被删除

Images created with factory faker get deleted when stored in storage/public/images folder

我试图用假图像填充我的数据库,但是当 faker 将它保存在我的图像文件夹中一秒钟后它被删除并且在我的数据库字段中我得到 0

可以访问http://lorempixel.com/,也能ping通

也试过这个:$faker->image(storage_path('app/public/images'), 800, 800, 'nature', false),

并像这里一样改变吸积木 url:https://github.com/fzaninotto/Faker/issues/1715 (http://placekitten.com/ and http://placekitten.com/)

这是我的 post 工厂代码:

<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Post;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence,
        'body' => $faker->text(600),
        'slug' => $faker->slug, 
        'metaTitle' => $faker->sentence,
        'metaDescription' => $faker->sentence,
        'user_id' => App\User::all(['id'])->random(),
        'postImage' => $faker->image('public/storage/images',640,480, 'nature', false),
        'visible' => $faker->boolean(85),
        'favorite' => $faker->boolean
    ];
});

它应该将图像存储在我的 storage/public/images 文件夹中并随机给我一个 slug.jpeg,但它会删除它并给出一个 0

编辑: 发现如果我 dd() the faker->image comand it returns false, dunno why...

确保在 storage/app/public 目录下有一个名为 images 的文件夹。

已创建符号 link:

php artisan storage:link

最后试试这个:

use App\Post;
use Illuminate\Http\File;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
    $image = $faker->image();
    $imageFile = new File($image);

    return [
        'title' => $faker->sentence,
        'body' => $faker->text(600),
        'slug' => $faker->slug, 
        'metaTitle' => $faker->sentence,
        'metaDescription' => $faker->sentence,
        'user_id' => App\User::all(['id'])->random(),
        'image' => Storage::disk('public')->putFile('images', $imageFile),
        'visible' => $faker->boolean(85),
        'favorite' => $faker->boolean
    ];
});

编辑

根据聊天中的讨论,这似乎是一个 cURL 问题。 Faker 在这一行失败 (Image.php):

$success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200;

然后,删除临时文件并返回 false 作为结果。