Laravel 4.2 polymorphic relation with image tables

I'm new to laravel and trying to understand correctly how to show in my view my picture of my relations.

For example I have a table for Category, Post, User, and Image, and I need some help with this.

For my DB it's OK with Image table like tutorial polymorphic relation on laravel docs.(same like doc)

In my models image I have my polymorphic relation and it's all OK

class Image extends \Eloquent {

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

    public function imageable(){

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

I need to associate a image on my different category

  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();
      }
}

After that in my homecontroller for my home page I have a request for 1 particular post to show with a specific category

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

In my home index view I have a foreach $headliner like this

  <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>

But I don't know how to show my image in my foreach because I have my post associated to category so it's OK but when I try to

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

(path is my entries in my DB) I have a error.

Thanks for your help.

Answers


As you've used morphMany() to define images relation for a category, loading this relation will give you a collection of Image objects. Therefore in your view $headline->images refers to this collection, not a single image. Collection does not have a path attribute, hence the error.

You'll need to iterate through your images collection in the view and then render each image separately, e.g.:

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

Need Your Help

XE2 exe and dcu location

delphi delphi-xe2

I am attempting to move to XE2 from Delphi 6.

Login and download all documents - Dropbox

python dropbox dropbox-api

Is it possible to give[parameters] username and password for Dropbox in python to download all documents in the disk ?