Question

I am currently trying to fake seed quite a vast table of mine. I am currently using Laravel 4.1.

I have one column which is set to NULL, because it only has to be filled if the previous column is set to '1', for example.

I currently have:

$alerts = 
[[
    'user_id' => $i,
    'reference' => $faker->sentence($nbWords = 1),
    'type' => rand(1,2),
    'category' => rand(1,4),
    'headline' => $faker->sentence($nbWords = 2),
    'description' => $faker->realText(150)               
]];

So, should 'type' be '1', I want the following two fields 'range_to' and 'range_from' to be then faked.

Is this possible logically? Any pointers would be hugely appreciated.

Was it helpful?

Solution

Split your carry using if statement

$alerts = 
[[
    'user_id' => $i,
    'reference' => $faker->sentence($nbWords = 1),
    'type' => rand(1,2),
    'category' => rand(1,4),
    'headline' => $faker->sentence($nbWords = 2),
    'description' => $faker->realText(150)               
]];

foreach($alerts as $key=>$alert){
    if( $alert['type'] != 1 ){
        $alerts[$key]['category'] = $alerts[$key]['headline'] = $alerts[$key]['description'] = NULL ;
    }
}

So you set your alerts as you want, then you loop on them and apply the condition you want if it exist then you edit the array.

in above example it will loop $alerts and for each array if ['type'] not equal 1 it will set category,headline and description of that array to NULL..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top