【问题标题】:How to chain call hasOne with morphMany?如何用 morphMany 链接调用 hasOne?
【发布时间】:2021-04-17 02:08:46
【问题描述】:

我认为概念关系应该很容易理解。只需与适当的外国建立关系和链条。 我尝试将关系 hasOne 与 morphMany 联系起来。但是 hasOne 无法访问变形关系

调用未定义的方法 Illuminate\Database\Eloquent\Relations\HasOne::images()

media model
public function poster(){
  return $this->hasOne(Media::class);
}

poster model
public function images(){
  return $this->morphMany(Image::class, 'imageable');
}

controller
$media = Media::first();

$media->poster()->images()->create();

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你不能这样链接,但你可以这样链接:

    $media->poster->images()->create()
    

    简称:

    $media->poster()->first()->images()->create()
    

    您首先需要有一个正确的poster 对象,然后才能访问images

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2018-12-06
      • 2017-08-26
      相关资源
      最近更新 更多