Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4, Multiple Polymorphic Relations from one Model

I'm trying to set up polymorphic relationships in Laravel 4 so that I can have one Image class which handles everything related to uploads, unlinks and so on, then have it used by multiple different Models. That's fine, until I get to trying to create multiple links from the same Model.

For example, I currently have something like this:

Models/Image.php

class Image extends Eloquent {
    public function of() { return $this->morphTo(); }
}

Models/Person.php

class Person extends Eloquent {
    public function mugshot() { return $this->morphOne('Image', 'of'); }
    public function photos() { return $this->morphMany('Image', 'of'); }
}

Models/Place.php

class Place extends Eloquent {
    public function photos() { return $this->morphMany('Image', 'of'); }
}

Here, a Person can upload one mugshot and many photos, while a Place can have many photos. The problem is that when I create a mugshot on a Person, it saves this into the images table in the database:

id: 1
of_id: 1
of_type: Person

It doesn't store the fact that it's a mugshot and not a photo, so when I go to retrieve it, $person->mugshot may sometimes return one of $person->photos and vice versa.

Is there either (a) a better way to do this than creating 2 links on the same Model, or (b) a way to actually make this way work?

like image 535
Joe Avatar asked Nov 07 '13 12:11

Joe


1 Answers

No built-in way right now. Maybe in Laravel 4.1 that's supposed to bring a complete rewrite of polymorphic relations.

Add a type property to Image, then define where conditions on the relations:

public function mugshot() { 
    return $this->morphOne('Image', 'of')->where('type', 'mugshot'); 
}

public function photos() { 
    return $this->morphMany('Image', 'of')->where('type', 'photo'); 
}

Don't forget to set type on Images you create. Or, like I did bellow, hide that logic inside the model.

Here's my code (I'm using PHP 5.4 with short array notation):

Image:

namespace SP\Models;

class Image extends BaseModel {

    const MUGSHOT = 'mugshot';
    const PHOTO = 'photo';

    protected $hidden = ['type'];

    public function of()
    {
        return $this->morphTo();
    }
}

Person:

namespace SP\Models;

use SP\Models\Image;

class Person extends BaseModel {

    public function mugshot() { 
        return $this->morphOne('SP\Models\Image', 'of')
                            ->where('type', Image::MUGSHOT); 
    }

    public function photos() { 
        return $this->morphMany('SP\Models\Image', 'of')
                            ->where('type', Image::PHOTO); 
    }

    public function saveMugshot($image)
    {
        $image->type = Image::MUGSHOT;
        $image->save();
        $this->mugshot()->save($image);
    }

    public function savePhotos($images)
    {
        if(!is_array($images))
        {
            $images = [$images];
        }

        foreach($images as $image)
        {
            $image->type = Image::PHOTO;
            $image->save();
            $this->photos()->save($image);
        }
    }
}

Somewhere in a controller/service:

$person->savePhotos([$image1, $image2]);
like image 180
Sergiu Paraschiv Avatar answered Oct 09 '22 17:10

Sergiu Paraschiv