Ad

Getting Unexpected Response From Laravel Factory() Causing Segmentation Fault (Core Dumped)

A Laravel factory() function inside a factory()->create()->each() is giving a very unexpected result. The function is returning a huge array full of strings and sub-arrays, when var_dumping it inside the console I have to ctrl+c to stop it from rendering the result else it keeps on running (haven't seen the end of the array). When executing php artisan DB:seed I'll the following result: Segmentation fault (Core dumped).

The content inside the array looks like data from the Faker library.

Does anyone has an idea of where this can possibly come from?

  • I have minimized my code down to only the factory for the Model I want to insert in my DB. This ofcourse still is giving me the same result.

  • I have var_dumped the class name from the object returned, the object returned is from the class "Illuminate\Database\Eloquent\FactoryBuilder". As expected.

  • Inside the SubscriptionFactory.php file I have commented out everything inside the $factory->define() function.

Code

The code below is giving me the never ending array.

factory(App\SubscriptionGroup::class, 3)
->create()
->each(function ($subscriptionGroup)
{
    $test = factory(App\Subscription::class);
    var_dump($test);
});

The code below is my SubscriptionFactory php file.

use Faker\Generator as Faker;

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

    $minAge = null;
    if (rand(0, 100) > 75) {
        $minAge = $faker->randomElement([12, 16, 18]);
    }

    $maxAge = null;
    if (rand(0, 100) > 75) {
        $maxAge = $faker->randomElement([21, 35, 50]);
    }

    $duration = 1;
    if (rand(0, 100) > 25) {
        $duration = $faker->randomElement([1, 6, 12, 24]);
    }

    $paidByMember = null;
    if (rand(0, 100) > 90) {
        $paidByMember = true;
    } elseif (rand(0, 100) > 90) {
        $paidByMember = false;
    }

    $tokenPeriod       = \App\TokenPeriod::find(rand(1, 7));
    $directDebitPeriod = \App\DirectDebitPeriod::find(rand(1, 3));

    $unlimitedAccess         = $faker->boolean(10);
    $periodicTokens          = 0;
    $transferableTokenPeriod = 0;

    if ( ! $unlimitedAccess) {
        $periodicTokens          = $faker->randomElement([1, 2, 4]);
        $transferableTokenPeriod = rand(0, 4);
    }

    $name = implode(' ', $faker->words(rand(1, 2)));

    return [
        'status'                    => 'published',
        'name'                      => $name,
        'min_age'                   => $minAge,
        'max_age'                   => $maxAge,
        'membership_duration'       => $duration,
        'membership_duration_in'    => 'months',
        'available_date'            => \Carbon\Carbon::now(),
        'withdraw_date'             => null,
        'visibility_order'          => 1,
        'paid_by_member'            => $paidByMember,
        'unlimited_access'          => $unlimitedAccess,
        'periodic_tokens'           => $periodicTokens,
        'transferable_token_period' => $transferableTokenPeriod,
        'check_in_access_type_id'   => 1,
        'token_period_id'           => $tokenPeriod->id,
        'direct_debit_period_id'    => $directDebitPeriod->id
    ];
});

Result

The SubscriptionGroup factory is running as expected and fills the database accordingly. But the Subscription factory is giving the wrong result. See the piece of the resulting array below.

...
["de schuitjes"]=>
array(1) {
[0]=>
  string(3) "van"
}
["schuitjes van"]=>
array(1) {
[0]=>
  string(3) "den"
}
["den ponton-steiger"]=>
array(1) {
[0]=>
  string(3) "bij"
}
["ponton-steiger bij"]=>
array(1) {
[0]=>
  string(9) "Nijmegen."
}
["bij Nijmegen."]=>
array(1) {
[0]=>
  string(2) "En"
}
["Nijmegen. En"]=>
array(1) {
[0]=>
  string(2) "nu"
}
["nu spraken"]=>
array(1) {
[0]=>
  string(2) "ze"
}
["ze over"]=>
array(2) {
[0]=>
  string(3) "z'n"
[1]=>
  string(2) "'t"
}
...

EDIT

The code below also seems to cause the Segmentation fault (core dumped) error. So it seems the error doesn't come from factory(App\Subscription::class), but rather the subscriptions() relation inside SubscriptionGroup. Which makes it even stranger because that's just a simple hasMany relationship.

factory(App\SubscriptionGroup::class)
->create()
->each(function ($subscriptionGroup)
{
        $subscriptionGroup->subscriptions();
});

The subscriptions() relation inside the SubscriptionGroup class:

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function subscriptions()
{
    return $this->hasMany('App\Subscription');
}
Ad

Answer

I have found the answer to my own problem.

For certain models I had created an abstract class with a $scope attribute. The attribute had a default value 'public'. After removing the default value from this attribute everything worked.

I've moved the default value for this attribute to the constructor method.

Ad
source: stackoverflow.com
Ad