phplaravelfakerlaravel-seeding

How can I declare $faker in the seed file in Laravel when overriding for argument to factory?


I am trying to create multiple model of seeds like seedt1, seedt2, seedt3 with parameters for the sample.

I am aware of factory states. I don't want to use it, I want to keep my factory model minimal and clean as possible.

I have my model factory:

<?php

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

use App\User;
use App\Client;
use App\Query;
use App\Task;
use Faker\Generator as Faker;

////////// Users factory //////////
$factory->define('App\User', function (Faker $faker) {
    $faker->locale = 'fr_FR';
    return [
        'full_name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => bcrypt('secret'),
        'remember_token' => Str::random(10),
        'objective' => '0',
        'role_id' =>1,
        'isActive' => '1',
        'img_path' =>'user-lg.jpg',
    ];
});

////////// Clients factory //////////
$factory->define('App\Client', function ($faker) {
    $faker->locale = 'fr_FR';
    return [
        'name' => $faker->company,
        'legalname' => $faker->name,
        'legalname2' => $faker->name,
        'legalname3' => $faker->name,
        'email' => $faker->email,
        'address' => $faker->address,
        'country' => $faker->country,
        'website' => $faker->domainName,
        'telephone' => $faker->phoneNumber,
        'telephone2' => $faker->phoneNumber,
        'fax' => $faker->PhoneNumber,
        'other1' => $faker->email,
        'other2' => $faker->email,
        'other3' => $faker->email,
        'foundeddate' => $faker->dateTimeThisDecade,
        'crmregistered' => $faker->dateTimeThisYear,
        'comments' => $faker->realText($maxNbChars = 300, $indexSize = 2),
        'insurancenumber' => $faker->numberBetween($min = 80000, $max = 150000),
        'data1' => $faker->boolean($chanceOfGettingTrue = 50),
        'data2' => $faker->boolean($chanceOfGettingTrue = 50),
        'type_id' => $faker->numberBetween($min = 1, $max = 2),
        'isActive' => '1',
        'user_id' => $faker->numberBetween($min = 3, $max = 15),
        'img_path' =>'imageUrl($width, $height, \'cats\')',
    ];
});
////////// Queries factory //////////
$factory->define('App\Query', function ($faker) {
    $faker->locale = 'fr_FR';
    return [
        'guests_no' => $faker->numberBetween($min = 50, $max = 500),
        'days_no' => $faker->numberBetween($min = 3, $max = 7),
        'value' => $faker->ean8,
        'arrival_date' => $faker->dateTimeInInterval($startDate = '+1 years', $interval = '+ 14 days', $timezone = null),
        'departure_date' => $faker->dateTimeInInterval($startDate = '+1 years', $interval = '+ 21 days', $timezone = null),
        'file_name' => $faker->numerify('dossier ######'),
        'file_number' => $faker->ean8,
        'facture_number' => $faker->creditCardNumber,
        'guide' => $faker->boolean($chanceOfGettingTrue = 50),
        'rentacar' => $faker->boolean($chanceOfGettingTrue = 50),
        'aerial' => $faker->boolean($chanceOfGettingTrue = 50),
        'user_id' => $faker->numberBetween($min = 3, $max = 15),
        'client_id' => $faker->numberBetween($min = 1, $max = 1000),
        'transport_type_id' => $faker->numberBetween($min = 1, $max = 3),
        'created_at' => $faker->dateTimeThisYear,
        'payment_status_id' => $faker->numberBetween($min = 1, $max = 3),
        'query_status_id' => $faker->numberBetween($min = 1, $max = 7),
        'query_type_id' => $faker->numberBetween($min = 1, $max = 3),
        'isActive' => '1',
        'notified' => $faker->boolean($chanceOfGettingTrue = 50),
    ];
});
////////// Task factory //////////
$factory->define('App\Task', function ($faker) {
    $faker->locale = 'fr_FR';
    return [
        'text' => $faker->realText($maxNbChars = 300, $indexSize = 2),
        'name' => $faker->name,
        'status' => $faker->ean8,
        'user_id' => $faker->numberBetween($min = 3, $max = 15),
        'query_id' => $faker->numberBetween($min = 1, $max = 500),
        'isActive' => '1',
        'query_status_id' => $faker->numberBetween($min = 1, $max = 7),
    ];
});
////////// State sample factory //////////
/*$factory->state(App\User::class, 'delinquent', function ($faker) {
    return [
        'account_status' => 'delinquent',
    ];
});*/

And my seed file:

<?php

use Illuminate\Database\Seeder;

class sampleT1 extends Seeder
{

    public function run()
    {

        factory('App\User', 1)->create(['role_id' =>'2',]); //1 financial user id 2
        factory('App\User', 3)->create(['role_id' =>'4',]); //3 managers id 3-5
        factory('App\User', 5)->create(); //10 users id 6-10
        factory('App\Client', 300)->create(['user_id' => $faker->numberBetween($min = 3, $max = 10),]); //300 clients
        factory('App\Query', 1000)->create(['user_id' => $faker->numberBetween($min = 3, $max = 10), 'client_id' => $faker->numberBetween($min = 1, $max = 300),]); //300 queries
        factory('App\Task', 5000)->create(['user_id' => $faker->numberBetween($min = 3, $max = 10), 'query_id' => $faker->numberBetween($min = 1, $max = 250),]); //5000 tasks
        //factory('App\User', 50)->states('premium', 'delinquent')->create();

    }
}

When I run my seed with:

php artisan db:seed --class=sampleT1

I get:

deployer@debdaddytp:/var/www/mice$ php artisan db:seed --class=sampleT1

   ErrorException 

  Undefined variable: faker

  at database/seeds/sampleT1.php:20

Obviously the variable of faker is not inherited from the model factory, how should I declare it in my seed for this to work?


Solution

  • Use Faker\Factory::create() to create and initialize a faker generator, which can generate data by accessing properties named after the type of data you want.

    $faker = Faker\Factory::create();
    

    And should work just fine. source.