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

Trying to get property of non-object Error i like

所以这是我这样做的问题:

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

你有一个 JSON 对象,所以你需要像这样解码它:

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

这样使用

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