试图获取非对象的属性错误我喜欢

时间:2016-05-31 16:01:20

标签: php laravel-5.2 laravel-blade

所以当我这样做时,问题就出现了:

<p>{{ $event->media }}</p>

我明白了:

{"id":43,"location":"\/assets\/img\/space-4k.png","description":"Space","image_album_id":1277165568,"featured":null,"thumbnail":null,"isVisible":1}

然后我想要这个位置,所以我这样做:

<p>{{ $event->media->location }}</p>

然后我得到了这个很好的尝试获得非对象错误的属性。

我把它换成了另一个对象并做了同样的事情并且它起作用了......所以我无法找到它为什么不能工作......

我的活动模型:

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;
    use DB;

    class Event extends Model
    {
    protected $table = "events";
    public $timestamps = false;

   public function albums()
   {
        return $this->belongsToMany('App\Album', 'events_has_image_albums', 'events_id', 'image_albums_id');
   }

    public function viewableAlbums()
    {
        return $this->belongsToMany('App\Album', 'events_has_image_albums', 'events_id', 'image_albums_id')
        ->whereExists(function($query)
        {
            $query->select(DB::raw(1))
                ->from("images")
                ->whereRaw('images.image_album_id = image_albums.id')
                ->where('isVisible', '=' , '1');
        })
        ->with('FirstMedia');
    }

    public function images()
    {
        return $this->belongsToMany('App\Media', 'events_has_images',    'events_id', 'images_id');
    }

    // this is the media function from the $event->media
    public function media()
    {
      return $this->belongsTo('App\Media', 'header');
    }
}

2 个答案:

答案 0 :(得分:0)

您拥有的是JSON Obect,因此您需要像这样对其进行解码:

     <?php $objDecoded  = json_decode($event->media); ?>
     <?php $strLocation = $objDecoded->location; ?>
     <?php var_dump($objDecoded); exit; // TRY TO DUMP THE DECODED DATA TO SEE WHAT YOU GET... ?>

     <p>{{ $strLocation }}</p>

答案 1 :(得分:0)

像这样使用

<p>{{ $event->media['location'] }}</p>