Seeding Grid Data
- Posted February 14, 2017
- Addon Tutorials
- PRO
- Advanced
Introduction
Seeding a Grid field is in many ways the same as seeding any other relationship. This tutorial will cover the slightly more complicated relationship structure of the Grid field type and how to seed data into it.
Relationship Structure
First let's take a look at the structure of the relationships that the Grid field type uses.
The relationship from the Entry
to the Grid Entry
looks like this:
Entry -> Grid Objects -> Grid Entry
In the above example the Entry -> Grid Objects
is a multiple relationship where every Grid Object
is a GridModel
instance with a polymorphic relationship to the Grid Entry
which holds the actual field data for the grid row.
Prerequisites
This tutorial will assume you have already seeded, migrated, or otherwise created the Grid structures required to seed.
Let's assume:
- The
users
stream has a grid field typefavorites
assigned to it. - We have a
books
grid withtitle
andauthor
fields assigned. - We also have a
movies
with thetitle
field assigned. - Both
books
andmovies
grids aretranslatable
. - In both grids the
title
field assignment istranslatable
.
Creating a Seeder
Let's create a seeder in the base application's /database/seeds
directory using Laravel's make:seeder
command:
php artisan make:seeder GridSeeder
The rest of the code provided in this article should be placed within the seeder's run
method.
Seeding Grid Data
The first thing we will need to do is grab a user to seed data for. Let's just grab the first user:
$user = UserModel::first();
Next let's create the Grid Entry
portion of the relationship. The name of the generated model to use here will depend on the name of the stream you are seeding:
$book = GridBooksEntryModel::create(
[
'en' => [
'title' => '7 Habits of Highly Effective People',
],
'author' => 'Stephen Covey',
]
);
$movie = GridMoviesEntryModel::create(
[
'en' => [
'title' => 'John Wick',
],
]
);
Next we will use the grid relation's create
method to create grid rows. This will create the Grid Object
portion of the relationship and also attach it to the Entry
:
$user->favorites()->create(
[
'entry' => $book,
]
);
$user->favorites()->create(
[
'entry' => $movie,
]
);
Running the Seeder
To run our seeder use Laravel's db:seed
command:
php artisan db:seed --class=GridSeeder
Example
Below is an example of what this seeder might look like all cleaned up and with steps combined:
<?php
use Anomaly\Streams\Platform\Model\Grid\GridBooksEntryModel;
use Anomaly\Streams\Platform\Model\Grid\GridMoviesEntryModel;
use Anomaly\UsersModule\User\UserModel;
use Illuminate\Database\Seeder;
class GridSeeder extends Seeder
{
public function run()
{
/* @var UserModel $user */
$user = UserModel::first();
$user->favorites()->create(
[
'entry' => GridBooksEntryModel::create(
[
'en' => [
'title' => '7 Habits of Highly Effective People',
],
'author' => 'Stephen Covey',
]
),
]
);
$user->favorites()->create(
[
'entry' => GridMoviesEntryModel::create(
[
'en' => [
'title' => 'John Wick',
],
]
),
]
);
}
}