vendredi 17 novembre 2017

Laravel - How get value from collection

I need get value from collection. I have 3 tables and models: Tours , Images, ImageTours. They have relationships:

Tours:

public function imagesTours(){
  return $this->belongsTo('App\ImageTour', 'id');
}

Images:

public function imagesTours(){
  return $this->belongsTo('App\ImageTour', 'id');
}

ImageTours:

public function images(){
    return $this->hasMany('App\Images', 'id');
    }
public function tours(){
    return $this->hasOne('App\Tours', 'id');
}

In the view:

@foreach($tours as $tour)
 @foreach($images as $image)
 <?php dd($picture = $image->images); ?>
 <td>
  <a href="">
   <span class=image>
    <img src='' alt width="200px"/>
   </span>
  </a>
 </td>
 @endforeach
@endforeach

But i need get value from collection $image. When i try: $picture = $image->images->name i have error: Property [name] does not exist on this collection instance. But in the table Images i have that field.

So if i use dd to $image->images->name i can look content of collection and field name; This is dd-log: http://ift.tt/2APdiGl



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire