mardi 17 janvier 2017

OctoberCMS Multiple morphOne Relationship

I have a polymorphic Image Model (public $morphTo = ['imageable' => []];) and a Post Model that uses it.

I want the Post to have 2 o 3 Images for very different purposes (Post Header, Featured Image and Background Image)

Due the complexity of the Image model handling images I would like very much to use it on Posts, but one image of each type, that's why I use morphOne. Did so like this on Post Model:

public $morphOne = [
    'header_image' => [
        Image::class,
        'name' => 'imageable'
    ],
    'featured_image' => [
        Image::class,
        'name' => 'imageable'
    ],
    'background_image' => [
        Image::class,
        'name' => 'imageable'
    ],
];

But recovering the image seems to always retrieve the first in the image table even having both saved there. Saving any replaces the first record, deleting deletes the first record, etc ...

I can guess the problem, probably retrieving ONE record for the MorphOne relationship gets the first that matches the typeand the id for the imageable attribute.

The question is: Is there a way to make this structure work ? Parameters on relation, maybe a different morph relationship ?

Thanks for any help!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2j706Ei
via IFTTT

Aucun commentaire:

Enregistrer un commentaire