Geoff
Geoff

Reputation: 6639

Faker laravel unknown formatter image

Am using faker in laravel and in my seeder i have

 public function run()
   {
    $faker = new Faker\Generator();
    //create a user
    $adminuser = App\User::create(
        [
            'name' => 'admin',
            'first_name' => 'firstuser',
            'profile_pic'=>$faker->image('storage/app/public/users',400,300, 'people', false),
        ]
    );

But now am getting an error

Unknown formatter "image"

Where am i going wrong

I just want to add an image to a user generated via faker

Upvotes: 0

Views: 6740

Answers (2)

Alihossein shahabi
Alihossein shahabi

Reputation: 4352

You must use the ‍‍Method Injection

for example :

<?php

use Illuminate\Database\Seeder;
use Faker\Generator as Faker;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @param Faker $faker
     * @return void
     */
    public function run(Faker $faker)
    {
        $adminuser = App\User::create(
            [
               'name' => $faker->name,
                'first_name' => $faker->firstName,
                'profile_pic' => $faker->image(public_path('img'),400,300, 'people', true),
            ]
        );
    }
}

Upvotes: 2

ljubadr
ljubadr

Reputation: 2254

From the docs on basic usage

public function run()
{
    // instead of using new Faker\Generator()
    $faker = Faker\Factory::create();

    //create a user
    $adminuser = App\User::create(
        [
            'name'        => 'admin',
            'first_name'  => 'firstuser',
            'profile_pic' => $faker->image('storage/app/public/users',400,300, 'people', false),
        ]
    );
}

Upvotes: 4

Related Questions