Assume we have a products table and there is a field like an image. Now we want to seed data in laravel using database seeder using faker laravel to generate some fake images for the products table. To do that, we can use image() helper. I will show you an example of laravel faker image url.

So if you are looking for laravel faker image returns false then this laravel 9 faker image tutorial is for you. So let's see the example code of laravel faker image returns false:

<?php

use App\Models\Product;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

        $faker = Faker::create();

        for ($i=1; $i < 101 ; $i++) { 
            $product = new Product;
            $product->image = $faker->image('public/storage/images',640,480, null, false);
            $company->save();
        }  
    }
}

 

If you are in Laravel 9 version, then you can use like this:

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
class ProductFactory extends Factory
{
    public function definition()
    {
        return [
            'name' => fake()->image('public/storage/images',640,480, null, false)
        ];
    }
}

 

Below Laravel 9 version, you can create faker image like:

<?php

use App\Product;
use Faker\Generator as Faker;

$factory->define(Product::class, function (Faker $faker) {
    return [
        'name' => $faker->word,
        'short_description' => $faker->sentence,
        'description' => $faker->paragraph,
        'category_id' => function () {
            return factory(App\Category::class)->create()->id;
        },
        'amount' => $faker->randomFloat(2, 0, 10000),
        'image' => $faker->image('public/storage/images',640,480, null, false),

    ];
});

 

Now print this image like:

<img src="/storage/images/{{$product->image}}">

 

See all the available faker image helpers from below:

// Image generation provided by LoremPixel (http://lorempixel.com/)
imageUrl($width = 640, $height = 480) // 'http://lorempixel.com/640/480/'
imageUrl($width, $height, 'cats')     // 'http://lorempixel.com/800/600/cats/'
imageUrl($width, $height, 'cats', true, 'Faker') // 'http://lorempixel.com/800/400/cats/Faker'
imageUrl($width, $height, 'cats', true, 'Faker', true) // 'http://lorempixel.com/gray/800/400/cats/Faker/' Monochrome image
image($dir = '/tmp', $width = 640, $height = 480) // '/tmp/13b73edae8443990be1aa8f1a483bc27.jpg'
image($dir, $width, $height, 'cats')  // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg' it's a cat!
image($dir, $width, $height, 'cats', false) // '13b73edae8443990be1aa8f1a483bc27.jpg' it's a filename without path
image($dir, $width, $height, 'cats', true, false) // it's a no randomize images (default: `true`)
image($dir, $width, $height, 'cats', true, true, 'Faker') // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg' it's a cat with 'Faker' text. Default, `null`.

 

Read also: Laravel Faker Company Name Example

 

Conclusion

I have tried to discuss the clear concept of laravel faker image url. Now we know laravel 9 faker image. Hope this laravel fake image tutorial will help you.