Im trying to create a news item and store its thumbnail at a location corresponding to its primary key: $this->getKey. This works on update since the key is available, but on create it is not.
I am using the manipulator setThumbnailImageAttribute to handle the file upload:
public function setThumbnailImageAttribute($value) {
$this->setImage(
'thumbnail_image',
$value,
'news/'.$this->getKey().'/thumbnail_image/'
);
}
setImage handles the upload and setting the correct value to the attribute (and so the database)
On update this results correctly in:
'news/1/thumbnail_image/'
On create this results incorrectly in:
'news//thumbnail_image/'
How can I achieve this with a mutator, or is there a different approach?
This is not achievable by your approach, yes! there are some nasty hacks to do so. But you've to manually call this method after the record has been created.