0

I have a database table look like this

|id |col2|col3|col4|
-------------------
| 1 |elm0|....|....|
| 2 |elm1|....|....|
| 3 |elm2|....|....|
|...|....|....|....|
|N+1|elmN|....|....|

I want to fill the col2 with data from array (not in random way). Example array:

$dataArray = array(elm0, elm1, elm2,...)

I created this factory:

<?php

use Faker\Generator as Faker;

$factory->define(App\Unit::class, function (Faker $faker) {

    $dataArray = array(elm0, elm1, elm2,...,elmN);

    return [
        'col2' => $dataArray[$index];
        'col3' => $faker->'whatever';
        'col4' => $faker->'whatever';
    ];

});

How I can do this?

MCMXCII
  • 1,043
  • 4
  • 13
  • 26
Yamona
  • 1,070
  • 1
  • 16
  • 36

4 Answers4

1

you can build like that:

<?php
use Faker\Generator as Faker;

$factory->define(App\Unit::class, function(Faker $faker) {

    $data = array(elm0, elm1, elm2,...,elmN);

    foreach($data as $kye=>$value) {
        $result['id'] = $key;
        $result['col2'] = $value;
        $result['col3'] = $faker->'whatever';
        $result['col4'] = $faker->'whatever';
    }

    return $result;
});

When you need to run an array, often foreach() solve your problem.

Hope that help you.

Cheers.

1

Had a similar problem and decided to skip the factory part and use only seeder. I got to the solution while reading this: Seed multiple rows at once laravel 5 answer by lukasgeiter.

First you make a seeder with:php artisan make:seeder UnitsTableSeeder

Then you will have in your seeder something like this:

<?php

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

class UnitsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
      $faker = new Faker;
      $data = ['elm0', 'elm1', 'elm2',...,'elmN'];
      $rows = [];

      foreach ($data as $element) {
        $rows[] = ['col2' => $element, 'col3' => $faker->'whatever', 'col4' => $faker->'whatever'];
      }

      App\Unit::insert($rows);
    }
}

After this you can seed your Units table like a sir :)

php artisan db:seed --class=UnitsTableSeeder

Likeas Ir
  • 11
  • 2
0
<?php

use Faker\Generator as Faker;

$factory->define(App\Unit::class, function(Faker $faker) {

        // Grab a random unit
        $unit = App\Unit::orderByRaw('RAND()')->first();

        // Or create a new unit
        $unit = factory(App\Unit::class)->create();

        return [
            'id' => $unit->id,
            'col2' => $faker->'whatever',
            'col3' => $faker->'whatever',
            'col4' => $faker->'whatever',
        ];
    });

Please check if it is work for you.

Jinandra Gupta
  • 546
  • 2
  • 9
0

If you want to get a random element from an array and fake it then you can do something as below:

$factory->define(Rule::class, function (Faker $faker) {

    $data = [
        'age' => [
            'borrower_age_min'  => 'min:21',
            'borrower_age_max'  => 'max:75'
        ],
        'affordability' => [
            'annual_income' => 'integer|min:40000',
            'loan_amount'   => 'integer|max:3*',
        ],
        'finance' => [
            'loan_length'   => 'integer|max:12',
            'loan_amount'   => 'integer|max:500000',
        ]
    ];

    return [
        'rule'     => json_encode([
            $faker->randomElement(
                [
                    $data['age']['borrower_age_min'],
                    $data['age']['borrower_age_max'],
                    $data['affordability']['annual_income'],
                    $data['affordability']['loan_amount'],
                    $data['finance']['loan_length'],
                    $data['finance']['loan_amount']
                ]
            )
        ])
    ];
});
Invincible
  • 1,418
  • 18
  • 23