apocalyarts's avatar

[L5.1] Insert images for new articles

Hi all,

I have a problem that bugs me, maybe you have an idea how I should build my application...

I have the Models Article and Image. I use Eloquents Polymorphic Relations for the Image Model and Article uses an ImageableTrait to include the functionality of having associated images, because in the future I will have more Models that should be "imageable". I also have an ImageController that returns an Image for a provided Image ID via response()->download($path).

Now each image can have a Thumbnail. This is easy. I add a File-Input to my Create-Form. After submitting the form I create the article, check if the Image is present in the Request an then do:

$article->setThumbnail(Input::file('image'));

My function (provided via the ImageableTrait) looks like this for now:

// $this->imagePath set to 'articles' in Model constructor

public function setThumbnail(UploadedFile $image){
    $prefix = str_random(5);
    $image->move(storage_path().'/img/'.$this->imagePath.'/', $prefix.$image->getClientOriginalName());
    $this->images()->where('type', 'Thumbnail')->delete();
    return $this->images()->create([
        'type' => 'Thumbnail',
        'path' => $this->imagePath.'/'.$prefix.$image->getClientOriginalName(),
        'imageable_id' => $this->id,
        'imageable_type' => get_class($this)
    ]);
}

My problem is: how can I implement additional images that will be included in the article textfield itself? I'm using the WYSIWYG-Editor Summernote and I can implement my own function for onImageUpload. But this would require some custom Code with AJAX and I don't know how to solve this bit, because I can't provide the ID of my article on image upload, because it obviously hasn't been created yet.

0 likes
0 replies

Please or to participate in this conversation.