Search code examples
phplaravelfaker

Images from PHPFaker got deleted when stored in storage/app/public/news folder


I have two tables news and images. These two tables have one-to-many relationship (one being news). I'm trying to make a factory on images, but right after I migrate with seed, the image saved to news directory but after one second it got deleted, and the path on the database returns news. I've read this question but laravel 8.4 (my current laravel project) uses phpfaker, so I guess it's now deprecated.

ImageFactory.php file

<?php

namespace Database\Factories;

use App\Models\Image;
use Illuminate\Database\Eloquent\Factories\Factory;

class ImageFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Image::class;

    /**
     * State dari gambarnya, 
     * pilih satu [news, packages, destinations]
     */
    public function news()
    {
        return $this->state(function (array $attributes) {
            return [
                'role' => 'news',
            ];
        });
    }
    public function packages()
    {
        return $this->state(function (array $attributes) {
            return [
                'role' => 'packages',
            ];
        });
    }
    public function destinations()
    {
        return $this->state(function (array $attributes) {
            return [
                'role' => 'destinations',
            ];
        });
    }

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'path' => 'news' . '/' . $this->faker->image(storage_path('app/public/news'), 2000, 1500, null, false),
        ];
    }
}

storage/app/public and storage/app/public/news has .gitignore file. I don't know if this is relevant or not, because my colleagues migrate the seed without any issues.


Solution

  • Referring to this comment, you just need to go to Images.php file on vendor PHPFaker and add these code

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

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