2

I need to create a seeder that will import data from a csv file. The csv file contains the path to an image file that needs to be attached to the model.

In the documentation we find:

You may also pass a string to the data attribute that contains an absolute path to a local file.

$model->avatar = '/path/to/somefile.jpg';

Unfortunately I might be missing something. This is the relevant code:

Model

public $attachOne = [
    'image' => 'System\Models\File'
];

Seeder

$product->image = '/path/image.png';
$product->save();

Error

The error is that the file is not being created. On top of that, no errors are shown in the migration log.

PS: When saving from an file upload field, everything works as expected.

  • Can you update the question with the error you're receiving when you run the seeder? – Joseph Feb 24 '17 at 09:09
  • @Joseph sure! The problem is being silently ignored. I have updated the question. – Daniel Teleginski Camargo Feb 24 '17 at 16:30
  • 1
    I'm getting exactly the same problem when I try it. However, following the example for multiple files works even if the relationship is attachOne: `$product->image()->create(['data' => '/path/image.png', 'is_public' => true]);` Perhaps it's a bug or the documentation is missing something? – Joseph Feb 25 '17 at 09:09
  • I'm interested in knowing if my suggestion had any effect? – B Faley Feb 27 '17 at 12:25
  • This apparently was a bug that was fixed in [build #402](http://octobercms.com/changelog): https://github.com/octobercms/docs/pull/227 – B Faley Feb 28 '17 at 07:13

1 Answers1

1

Try this one:

$file = new File;
$file->data = '/path/to/somefile.jpg';

$product->image = $file;
$product->save();
B Faley
  • 17,120
  • 43
  • 133
  • 223