3

I'm working on a business website with Laravel. I'm trying to generate fake images with a factory to make tests easier but it's not working because I don't know how to add the path of my images in the code.

The code which generate fake data in database:

I saw on youtube a training video in which we attach media information to a Laravel application through a package spatie/laravel-medialibrary but it's not clear.

Can anyone please explain this.

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44
Ramses Kouam
  • 311
  • 1
  • 8
  • 15

3 Answers3

1

You are creating the image using faker in the correct way.

You will need to run the following command:

php artisan storage:link

Following that, you can access the image using:

<img src="/storage/images/{{$product->image}}">
Alex
  • 878
  • 3
  • 16
  • 34
  • just to better understand, where does laravel take images to integrate in my application? from internet or what? – Ramses Kouam Jan 02 '20 at 18:57
  • The Faker library takes images from the internet, specifically from http://lorempixel.com/ You can put images in your public/ folder within your project and use them as well. They would be accessed in the same way, you would just need to make sure you specify the correct path. – Alex Jan 03 '20 at 10:05
  • i've tried it and it works, now i want to use a folder in which there's images in my computer to do the same operation, what can i do? – Ramses Kouam Jan 03 '20 at 19:55
  • You'd need to copy the images into your Laravel project. You should have a public folder, create a new folder in that called images and copy the images into that. You then just need to specify the file location then to load the image. – Alex Jan 04 '20 at 11:44
  • i did it but it doesn't work, i've changed the "public/storage/images" path into this path: "public/img" then i executed the factory method in laravel tinker to upload in my database but it always takes images from lorempixel – Ramses Kouam Jan 04 '20 at 15:07
  • 1
    Ah, I understand what you mean now. I don't think you can tell faker to use your own images. You would need to write your own logic to randomly choose images from that folder. Take a look at this to implement something: https://stackoverflow.com/questions/41166308/laravel-how-to-get-random-image-from-directory – Alex Jan 05 '20 at 13:04
0

This is my answer

function random_pic($dir)
{
    $files = glob($dir . '/*.*');
    $file = array_rand($files);
    return $files[$file];
}

$factory->define(Business::class, function (Faker $faker) {
    return [
        //
        'name' => $faker->name,
        'license' => random_pic('public/tmp/license')
    ];
});


Ray Zion
  • 610
  • 10
  • 11
0

This worked for me in Laravel 8.

The value of imageis the path on my local machine to store uploaded images in the client directory. When I ran this--App\Models\Client::factory()->create()-- in tinker an image was uploaded to the clients directory, and the image file name was stored in the database (see below). image(), I believe, requires five arguments.

enter image description here

enter image description here

<?php

namespace Database\Factories;

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

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'first_name'  => $this->faker->firstName,
            'last_name'   => $this->faker->lastName,
            'email'       => $this->faker->unique()->safeEmail,
            'phone'       => $this->faker->phoneNumber,
            'image'       => $this->faker->image('public/assets/images/uploaded/clients', 400, 300, null, false),
            'address'     => $this->faker->streetAddress,
            'city'        => $this->faker->city,
            'state'       => $this->faker->stateAbbr,
            'zip'         => $this->faker->postcode,
            'country'     => $this->faker->country,
            'description' => $this->faker->paragraph
        ];
    }
}
JimB814
  • 510
  • 8
  • 24