DB structure:
items ( id, ... )
photos ( id, ... )
comments ( id, entity_id, entity_type )
where entity_type
is an ENUM ('Item', 'Photo')
Models:
class Item extends Eloquent {
public function comments() {
return $this->morphMany('Comment', 'entity');
}
}
class Photo extends Eloquent {
// same as Item
}
class Comment extends Eloquent {
public function entity() {
return $this->morphTo();
}
}
For some reason when I try this:
$comments = $item->comments()->orderBy('created_at', 'asc')->get();
I get this error.
Call to undefined method Illuminate\Database\Query\Builder::getMorphClass()
It seems like it's trying to use the Query Builder instead of MorphOneOrMany, which does have getMorphClass defined. But even if I simply do $item->comments
without any further query building, it gives the same error.