Create module elements with a seeder
Created 7 years ago by araminho

Is there a way to create module elements with a seeder? For example, I created a ‘videos’ module which has its categories. And I would like to create a few categories and videos with a seeder. I would appreciate an example. Thanks.

squatto  —  7 years ago Best Answer

I have a "markets" module, and created a seeder for a "MarketType" stream within it:

/markets-module/src/MarketsModuleSeeder.php

<?php namespace Freemotion\MarketsModule;

use Anomaly\Streams\Platform\Database\Seeder\Seeder;
use Freemotion\MarketsModule\MarketType\MarketTypeSeeder;

class MarketsModuleSeeder extends Seeder
{
    /**
     * Run the seeder.
     */
    public function run()
    {
        $this->call(MarketTypeSeeder::class);
    }
}

/markets-module/src/MarketType/MarketTypeSeeder.php

<?php namespace Freemotion\MarketsModule\MarketType;

use Anomaly\Streams\Platform\Database\Seeder\Seeder;
use Freemotion\MarketsModule\MarketType\Contract\MarketTypeRepositoryInterface;

class MarketTypeSeeder extends Seeder
{
    /**
     * @var MarketTypeRepositoryInterface
     */
    private $marketTypeRepository;

    /**
     * MarketTypeSeeder constructor.
     *
     * @param MarketTypeRepositoryInterface $marketTypeRepository
     */
    public function __construct(MarketTypeRepositoryInterface $marketTypeRepository)
    {
        $this->marketTypeRepository = $marketTypeRepository;
    }

    /**
     * Run the seeder.
     */
    public function run()
    {
        $this->marketTypeRepository->truncate();

        $marketTypes = [
            'Health Clubs',
            'Education',
            'YMCA/JCC',
            'Community Centers',
            'Corporate Wellness',
            'Parks & Rec',
            'Government',
            'Multi-Housing',
        ];

        foreach ($marketTypes as $marketType) {
            $this->marketTypeRepository->create([
                'name'         => $marketType,
                'slug'         => str_slug(str_replace('/', '-', $marketType)),
                'description'  => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, ' .
                                  'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' .
                                  'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ' .
                                  'ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit ' .
                                  'esse cillum dolore eu fugiat nulla pariatur.',
                'long_tagline' => "This is the tagline for the market type $marketType",
                'tile_text'    => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' .
                                  'Sed congue ligula leo, quis venenatis tortor cursus semper.',
            ]);
        }
    }
}

Then you just run (from the root of your project): php artisan db:seed --addon=markets

araminho  —  7 years ago

Thanks squatto for a detailed and complete answer!