如何从集合创建模型? Laravel 5.5

时间:2018-08-22 11:19:40

标签: laravel-5.5

我有一家工厂

<allow-intent href="ws:*" />

但是该工厂返回了集合,但是我需要为测试建立模型。

$factory->define(App\Models\Polygon::class, function (Faker $faker) {
    return [
        'id' => 1,
        'sort' => 2,
        'name' => 'МКАД',
        'center' => '55.73,37.75',
        'points' => '[55.8977029,37.6724091],[55.8966419,37.6752415],
        'parent_id' => 'N20N',
        'location_id' => 'N1N',
        'lat' => 55.73,
        'lon' => 37.75,
        'xml_id' => 'N1N'
    ];
});

Laravel中是否有任何方法可以让您将集合转换为模型?

1 个答案:

答案 0 :(得分:0)

Laravel默认返回一个模型,其中包括:

public function testGetRouteDistance()
{
    $polygon = factory(Polygon::class)->make();

    // Use model in tests...

}

当您提供第二个参数时,将返回一个集合:

public function testGetRouteDistance()
{
    $polygon = factory(Polygon::class, 3)->make();

    // Use model in tests...

}