dongxun7962 2014-08-26 18:29
浏览 86

调用未定义的方法Illuminate \ Database \ Query \ Builder :: getMorphClass()

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.

  • 写回答

2条回答 默认 最新

  • duan5731 2014-08-26 19:10
    关注

    Here is my temporary solution

    class Item extends Eloquent {
      public function comments() {
        return $this->hasMany('Comment', 'entity_id')
          ->where('entity_type', '=', 'Item');
      }
    }
    

    I would really prefer to use morphMany though.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化