Laravel Eloquent setting a default value for a model relation?

Viewed 5921

I have two models:

class Product extends Eloquent {

    ...

    public function defaultPhoto()
    {
        return $this->belongsTo('Photo');
    }

    public function photos()
    {
        return $this->hasMany('Photo');
    }
}

class Photo extends Eloquent {

    ...

    public function getThumbAttribute() {
        return 'products/' . $this->uri . '/thumb.jpg';
    }

    public function getFullAttribute() {
        return 'products/' . $this->uri . '/full.jpg';
    }

    ...

}

This works fine, I can call $product->defaultPhoto->thumb and $product->defaultPhoto->full and get the path to the related image, and get all photos using $product->photos and looping through the values.

The problem arises when the product does not have a photo, I can't seem to figure out a way to set a default value for such a scenario.

I have tried doing things such as

public function photos() 
{
    $photos = $this->hasMany('Photo');
    if ($photos->count() === 0) {
        $p = new Photo;
        $p->url = 'default';
        $photos->add($p);
    }

    return $photos;
}

I have also creating a completely new Collection to store the new Photo model in, but they both return the same error:

Call to undefined method Illuminate\Database\Eloquent\Collection::getResults()

Has anyone done anything similar to this?

Thanks in advance!

1 Answers
Related