Marcolino922's avatar

Get thumb poliform image

Hi, I would like to get a single image (if the user has uploaded one or more images) and I created this function, but I think I'm wrong with the polymorphy.

Item.php

function getThumb()
    {
        if ($this->thumb()->exists()) {
            return 'img/annuncio/'.$this->id.'/'.$this->filename;
        } else {
            return 'img/components/no_images.jpg';
        }
    }

public function thumb() 
    {
        return $this->morphOne(Image::class, 'imageable');
    }

Image.php

public function imageable()
    {
        return $this->morphTo();
    }
0 likes
1 reply
Marcolino922's avatar

I solved it, it was a simple forgetfulness:

return 'img/annuncio/'.$this->id.'/'.$this->thumb->filename;

Please or to participate in this conversation.