Laravel & Lumen Polymorphic Relations seeder

Se pueden crear tablas con relaciones polimorficas con Laravel de una form muy sencilla, un ejemplo de implementacion de las mismas.

<?php

use Ramsey\Uuid\Uuid;

$factory->define(App\Models\Post::class, function ($faker) {
    $taggable = [
        App\Models\PostVideo::class,
        App\Models\PostAudio::class,
        App\Models\PostText::class,
        App\Models\PostImage::class,
        App\Models\PostYoutube::class,
        App\Models\PostSoundCloud::class,
    ];
    return [
        'title' => $faker->catchPhrase,
        'body' => $faker->text,
        'uuid' => Uuid::uuid4(),
        'hub_id' => rand(1,50),
        'owner_id' => rand(1,50),
        'votes' => rand(0,200),
        'plays' => rand(0,200),
        'featured' => $faker->boolean,
        'active' => $faker->boolean,
        'public' => $faker->boolean,
        'upload_ip' => $faker->ipv4,
        'post_id'   => 1,
        'post_type' => $faker->randomElement($taggable),
    ];
});


$factory->defineAs(App\Models\Post::class, 'audio', function (Faker\Generator $faker) use ($factory) {
    $follow = $factory->raw(App\Models\Post::class);
    $extras = [
        'post_id'   => $faker->randomElement(App\Models\PostAudio::pluck('id')->toArray()),
        'post_type' => App\Models\PostAudio::class,
    ];

    return array_merge($follow, $extras);
});

$factory->defineAs(App\Models\Post::class, 'image', function (Faker\Generator $faker) use ($factory) {
    $follow = $factory->raw(App\Models\Post::class);
    $extras = [
        'img_thumb' => $faker->imageUrl(300, 300),
        'img_cover' => $faker->imageUrl(1900, 450),
        'post_id'   => $faker->randomElement(App\Models\PostImage::pluck('id')->toArray()),
        'post_type' => App\Models\PostImage::class,
    ];

    return array_merge($follow, $extras);
});

$factory->defineAs(App\Models\Post::class, 'soundcloud', function (Faker\Generator $faker) use ($factory) {
    $cloudFiles = [
        'https://soundcloud.com/mr-mark-scott/long-live-the-chief-freestyle-mark-scott',
        'https://soundcloud.com/gusgus/biggi-veira-dj-set-queretaro-part2',
        'https://soundcloud.com/gusgus/sets/deep-inside-demos',
        'https://soundcloud.com/henry-saiz/sets/henry-saiz-for-days-and-nights',
        'https://soundcloud.com/futureclassic/flume-chet-faker-drop-the-3',
        'https://soundcloud.com/wknd/chet-faker-x-wknd-lover',
        'https://soundcloud.com/nickraymondg/flume-chet-faker-drop-the-game-the-golden-pony-remix-free-download',

    ];
    $follow = $factory->raw(App\Models\Post::class);
    $extras = [
        'url' => $faker->randomElement($cloudFiles),
        'post_id'   => $faker->randomElement(App\Models\PostSoundCloud::pluck('id')->toArray()),
        'post_type' => App\Models\PostSoundCloud::class,
    ];

    return array_merge($follow, $extras);
});

$factory->defineAs(App\Models\Post::class, 'text', function (Faker\Generator $faker) use ($factory) {
    $follow = $factory->raw(App\Models\Post::class);
    $extras = [
        'post_id'   => $faker->randomElement(App\Models\PostText::pluck('id')->toArray()),
        'post_type' => App\Models\PostText::class,
    ];

    return array_merge($follow, $extras);
});

$factory->defineAs(App\Models\Post::class, 'video', function (Faker\Generator $faker) use ($factory) {
    $follow = $factory->raw(App\Models\Post::class);
    $extras = [
        'post_id'   => $faker->randomElement(App\Models\PostVideo::pluck('id')->toArray()),
        'post_type' => App\Models\PostVideo::class,
    ];

    return array_merge($follow, $extras);
});

$factory->defineAs(App\Models\Post::class, 'youtube', function (Faker\Generator $faker) use ($factory) {
    $youFiles = [
        'https://www.youtube.com/watch?v=9tBOtSPrNik',
        'https://www.youtube.com/watch?v=UDVtMYqUAyw',
        'https://www.youtube.com/watch?v=rE7GU7SLguo',
        'https://www.youtube.com/watch?v=XINlEYXA3k0',
        'https://www.youtube.com/watch?v=5ANlWQy7-I0',
    ];
    $follow = $factory->raw(App\Models\Post::class);
    $extras = [
        'url' => $faker->randomElement($youFiles),
        'post_id'   => $faker->randomElement(App\Models\PostYoutube::pluck('id')->toArray()),
        'post_type' => App\Models\PostYoutube::class,
    ];

    return array_merge($follow, $extras);
});