从 Laravel factory() 获得意外响应导致分段错误(核心已转储)

Getting unexpected response from Laravel factory() causing Segmentation Fault (Core Dumped)

factory()->create()->each() 中的 Laravel factory() 函数给出了非常意外的结果。该函数返回一个充满字符串和子数组的巨大数组,当它在控制台中 var_dumping 时,我必须按 ctrl+c 来阻止它呈现结果,否则它会保持 运行(避风港t 看到数组的末尾)。执行 php artisan DB:seed 时,我将得到以下结果:Segmentation fault (Core dumped).

数组中的内容看起来像是来自 Faker 库的数据。

有人知道这可能来自哪里吗?

代码

下面的代码给出了永无止境的数组。

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

下面的代码是我的 SubscriptionFactory php 文件。

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
    ];
});

结果

SubscriptionGroup 工厂如预期的那样 运行 并相应地填充数据库。但是订阅工厂给出了错误的结果。请参阅下面的结果数组部分。

...
["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"
}
...

编辑

下面的代码似乎也导致了 Segmentation fault (core dumped) 错误。所以看起来错误不是来自 factory(App\Subscription::class),而是来自 SubscriptionGroup 内的 subscriptions() 关系。这让它变得更加奇怪,因为那只是一个简单的 hasMany 关系。

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

SubscriptionGroup class:

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

我找到了自己问题的答案。

对于某些模型,我创建了一个带有 $scope 属性的抽象 class。该属性有一个默认值 'public'。从此属性中删除默认值后,一切正常。

我已将此属性的默认值移至构造方法。