How to seed pages in PyroCMS with grids

In grids, create a wysiwyg field and name it content. Then create a grid and name it text. Assign the content field to it. Finally in pages, create a new field called Main Content and assign it to your page.

Scaffold a seed by running the following artisan command:
php artisan make:seeder GridSeeder

You will find GridSeeder.php within your project_directory/databases/seed directory.

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

linkedinfacebook