How to seed pages in PyroCMS with grids

By: William
Category: PyroCMS
Tags: medium

It's extremely easy to seed a complex setup of pages and grids with PyroCMS. Here is a very simple example of how i did it to get you started.

First, go to grids and create a fieldtype of wysiwyg. Give it the name content and the slug content.

Then create a new grid called text, with the slug text.

Now go to pages and create a new field called Main Content with the slug main_content. Then go to your page type, and assign this field to it.

Now you will have to create a seed, you easily scaffold a seed in PyroCMS by : php artisan make:seeder GridSeeder

If you look in root/databases/seed , you will now find GridSeeder.php.

I have mostly copy and pasted code from this page, describing it much better: PyroCMS Website

The difference between what he describes, and what we are doing is that we are seeding a page.

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Log;
use Anomaly\PagesModule\Page\Contract\PageRepositoryInterface;
use Anomaly\PagesModule\Type\Contract\TypeRepositoryInterface;
use Anomaly\Streams\Platform\Model\Grid\GridTextEntryModel;
use Anomaly\PagesModule\Page\PageModel;

class GridSeeder extends Seeder
{
    
    /**
     * The page repository.
     *
     * @var PageRepositoryInterface
     */
    protected $pages;

    /**
     * The types repository.
     *
     * @var TypeRepositoryInterface
     */
    protected $types;

    
    /**
     * Create a new PageSeeder instance.
     *
     * @param PageRepositoryInterface $pages
     * @param TypeRepositoryInterface $types
     */
    public function __construct(PageRepositoryInterface $pages, TypeRepositoryInterface $types)
    {
        $this->pages = $pages;
        $this->types = $types;
    }

    

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Define the slug of your pagetype that has your grid fieldtype
        // assigned to it. If you have simply assigned it to the default
        // pagetype that comes along with pyro, its default.
        $pageType = $this->types->findBySlug('default');

        // See https://pyrocms.com/help/addon-tutorials/grid-field-type/seeding-grid-data
        // But we are basically adding data to our grid here. If you have
        // created your grid using the GUI as i have done, you will find this
        // Modelname in root/storage/streams/APP_REF/models/Grid
        $text = GridTextEntryModel::create([
            'en'     => [
                'locale' => 'en',
            ],
            'content' => 'Test Content'
        ]);

        
        // Let's create the page! Again, see the page i ref above if you want
        // more information. But here you will setup your page with the necessary
        // data.
        $pageData = [
            'title'     => 'We will make the web great again!',
            'slug'      => str_slug('We will make the web great again!'),
            ..... and other stuff

        ];
        $page = $this->pages->create(
            [
                'en'           => [
                    'title' => $pageData['title']
                ],
                'slug'         => $pageData['slug'],
                'entry'        => $pageType->getEntryModel()->create(),
                'type'         => $pageType,
                'enabled'      => true,
                'home'         => false,
                'theme_layout' => 'theme::layouts/default.twig',
            ]
        )->allowedRoles()->sync([]);


        $pageObject = PageModel::where('slug',$pageData['slug'])->first();
        
        $pageObject->entry->mainContent()->create([
            'entry'=>$text
        ]);

        // Our a cleaner way of doing the last part:
        $pageObject->entry->mainContent()->create([
            'entry'=> GridTextEntryModel::create([
                'en'     => [
                    'locale' => 'en',
                ],
                'content' => 'Test Content'
            ]),
        ]);

    }
}

Now you are all ready to seed it! Just run: php artisan db:seed --class=GridSeeder