Laravel 5.4

时间:2017-07-04 14:41:45

标签: php laravel laravel-5.4 laravel-seeding

我正在尝试使用以下关系在Laravel 5.4中设置模型工厂:

基金
-----订阅
-----------订阅者

每个基金都有许多订阅,每个订阅者也有许多订阅。订阅属于基金,也属于订阅者。

我正在尝试在Laravel(Eloquent)建立一个模型工厂,以在一个播种机中完成这种关系。以下是我到目前为止的情况:

应用\基金

public function subscriptions()
{
    return $this->hasMany('App\Subscription');
}

public function subscribers()
{
    return $this->hasMany('App\Subscriber');
}

应用\订阅

public function subscriber()
{
    return $this->belongsTo('App\Subscriber');
}

public function fund()
{
    return $this->belongsTo('App\Fund');
}

应用\订户

public function subscriptions()
{
    return $this->hasMany('App\Subscription');
}

工厂\ App \ Fund

$factory->define(App\Fund::class, function (Faker\Generator $faker) {
return [
    'code' => strtoupper($faker->lexify('?????')).$faker->numberBetween('10000', '99999'),
    'name' => $faker->text('45'),
    'status' => 'Active',
    'currency' => $faker->currencyCode(),
    'location' => $faker->country(),
    'redeemable' => $faker->boolean(50),
    'interest_rate' => $faker->randomFloat(2, 0, 0.99),
    'interest_frequency' => 'Quarterly',
    'management_fee' => $faker->randomFloat(2, 0, 0.99),
    'inclusion_rate' => $faker->randomFloat(2, 0, 0.99),
    'notes' => $faker->sentences(6, TRUE),
];
});

\ App \ Subscription

的工厂
$factory->define(App\Subscription::class, function (Faker\Generator $faker) {
return [
    'code' => $faker->numberBetween('10000', '99999'),
    'amount' => $faker->randomFloat(2, 2000, 18000),
    'purchase_price' => $faker->randomFloat(2, 5, 25),
    'qty_purchased' => $faker->randomFloat(4, 1000, 20000),
    'date' => date("Y-m-d H:i:s"),
];
});

\ App \ Subscriber的工厂

$factory->define(App\Subscriber::class, function (Faker\Generator $faker) {
return [
    'name' => $faker->name,
    'email' => $faker->freeEmail,
    'phone' => $faker->phoneNumber,
    'fax' => $faker->phoneNumber,
    'address' => $faker->address,
];
});

FundSeeder.php

public function run()
{
    factory(App\Fund::class, 15)->create()->each(function ($u) {
        $u->subscriptions()->save(factory(App\Subscription::class)->make());
        $u->subscribers()->save(factory(App\Subscriber::class)->make());
    });
}

但是,当我运行php artisan db:seed时,会返回以下错误:

http://i.imgur.com/FFt6gFt.png

这是正确的,因为我不希望在Subscribers表上有fund_id值。基金有许多订阅,订阅者有许多订阅。

真正的问题是我无法弄清楚如何一次创建所有三个模型而不会导致错误。

对我来说这样做是有意义的:

public function run()
{
    factory(App\Fund::class, 2)->create()->each(function ($u) {
        $u->subscriptions()->save(factory(App\Subscription::class)->make({
            $u->subscribers()->save(factory(App\Subscriber::class)->make());
        }));

    });
}

然而,这导致Syntax Error - Unexpected {

您如何建议我继续这个?

1 个答案:

答案 0 :(得分:0)

关于如何设置关系并不是很清楚,但据我所知,订阅者需要添加到订阅中,因此可能会这样做:

public function run()
{
    factory(App\Fund::class, 15)->create()->each(function ($u) {
        $u->subscriptions()->save(factory(App\Subscription::class)->make());
        $subscription = App\Subscription::latest()->get()->last();
        $subscription->subscribers()->save(factory(App\Subscriber::class)->make());
    });
}