Seeding files-field_type
Created 7 years ago by kaarme

Trying to add a seeder and add files to my stream. But it complains about entry id as well. What is the correct way to seed files field type?

$autojen = $this->autojen->create(
    [
        'en'           => [
            'name' => 'test',
        ],
        'slug'         => str_slug('test'),
        'entry'        => $type->getEntryModel()->create(
            [

                    'speaker' => 'test',
                    'files' => [1],

            ]
        ),
        'type'         => $type,

    ]
);
ryanthompson  —  7 years ago

Files and other multiple field types (require a pivot table) need the entry created BEFORE attaching relations.

Try something like:

$autojen->files = [1]; // After you have created the main entry.

Or this will work too:

$autojen->files()->create($data); // These field types create native Laravel relations so feel free to use them.
kaarme  —  7 years ago

Thanks for your time @ryanthompson , but i can't get it to work. Tried a third way too.

$aj = $this->autojen->create(
    [
        'en'           => [
            'name' => 'test',
        ],
        'slug'         => str_slug('test'),
        'entry'        => $type->getEntryModel()->create(
            [

                    'speaker' => 'test',

            ]
        ),
        'type'         => $type,

    ]
);

// Tried this -> Didnt work
$aj->files=[1];
$aj->save();

// Tried this -> Didn't work
$aj->files()->create([1]);

// Tried this -> Didn't work
$ajEntry = $aj->getEntry();
$ajEntry->files()->create([1]);
ryanthompson  —  7 years ago

Try $aj->files()->sync([1]).

Perhaps take a look at the files() relation provided and go from there.

$aj->setAttribute('files', [1]); // Is another possible use (and for other fields as well.
kaarme  —  7 years ago

This finally works @ryanthompson :

// This works
$aj->entry->files()->sync([1]);

Now i have other issues. But that's for another thread.