【问题标题】:Laravel 4.2 polymorphic relation with image tablesLaravel 4.2 与图像表的多态关系
【发布时间】:2016-03-05 15:20:04
【问题描述】:

我是 laravel 的新手,并试图正确理解如何在我的视图中显示我的关系图。

例如,我有一个类别、帖子、用户和图像的表,我需要一些帮助。

对于我的数据库,可以使用 Laravel 文档上的教程多态关系之类的图像表。(与文档相同)

在我的模型图像中,我有我的多态关系,一切正常

class Image extends \Eloquent {

    protected $guarded = ['id' , 'created_at'];

    public function imageable(){

        return $this->morphTo();;
    }
}

我需要在我的不同类别中关联一张图片

  class Category extends \Eloquent {

      protected $guarded = ['id' , 'created_at'];

      public function posts(){
          return $this->hasMany('Post');
      }

      public function images(){

          return $this->morphMany('Image','imageable');
      }

      public function parent(){

          return $this->belongsTo('Category','parent_id');
      }

      public function children(){

          return $this->hasMany('Category','parent_id');
      }

      public function getImportantPost(){

          return $this->hasOne('Post')->latest();
      }

      public function latestPost(){
          return $this->hasOne('Post')->latest();
      }
}

在那之后,在我的主页控制器中,我请求 1 个特定帖子以特定类别显示

//show headliner post specific category
$headliner = Category::with(['posts' => function($query){
    $query->orderBy('published_at', 'DESC')->take(1)->get();
}])->where('id' , '=' , '5')->get();

在我的主页索引视图中,我有一个像这样的 foreach $headliner

  <div class="widget-area-4">
        <div class="widget kopa-article-list-widget">
            <ul class="clearfix">

            @foreach($headliner as $headline)
                <li>
                    <article class="entry-item clearfix">
                        <div class="entry-thumb">
                            <img src="{{$headline}}" alt="" />
                            <span class="category-title">{{$headline->name}}</span>
                        </div>
                        <!-- entry-thumb -->

                        <div class="entry-content">
                            <header>
                                <h5 class="entry-title"><a href="#">{{$headline->latestPost->name}}</a></h5>
                                <span class="entry-date"><i class="fa fa-calendar"></i> {{$headline->latestPost->published_at}}</span>
                                <span class="entry-categories">Auteur: <a href="#"></a></span>
                                <span class="entry-comments"><a href="#">{{$headline->latestPost->counts_comments}}</a><span></span></span>
                            </header>
                            <p>{{str_limit($headline->latestPost->content, 600 ,' ...' )}}</p>
                            <a class="more-link" href="#">Voir plus</a>
                        </div>
                        <!-- entry-content -->
                    </article>
                    <!-- entry-item -->
                </li>
                            @endforeach
            </ul>
        </div>
        <!-- kopa-article-list-widget -->
    </div>

但我不知道如何在我的 foreach 中显示我的图片,因为我的帖子与类别相关联,所以没关系,但是当我尝试时

{{$headline->images->path}}

(路径是我在数据库中的条目)我有一个错误。

感谢您的帮助。

【问题讨论】:

  • 第一个问题 - 错误是什么?

标签: php laravel laravel-4 orm eloquent


【解决方案1】:

当您使用 morphMany() 为类别定义 images 关系时,加载此关系将为您提供 Image 的集合对象。因此,在您看来 $headline->images 指的是这个集合,而不是单个图像。集合没有 path 属性,因此出现错误。

您需要遍历视图中的图像集合,然后分别渲染每个图像,例如:

@foreach($headline->images as $image)
  <img src="{{ $image->path }}" />
@endforeach

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 2019-08-20
    • 2018-10-18
    • 2018-03-08
    • 1970-01-01
    • 2018-10-20
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多