2016-05-31 64 views
2

所以這裏的問題,當我這樣做:試圖讓非對象錯誤的財產,我喜歡

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

然後我得到這個很好嘗試獲取非對象錯誤的屬性。

我DIT到其他的對象,也做了同樣的事情,它的工作..所以我找不到爲什麼它不會工作..

我的事件模型:

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

我打算繼續,猜測$ event-> media是JSON字符串而不是對象。什麼'print_r($ event-> media)'說? – apokryfos

+0

歡迎來到Stack Overflow!爲了讓你的問題更容易閱讀,請[格式化你的代碼和錯誤輸出](// stackoverflow.com/help/formatting),特別是每個代碼/輸出行前加4個空格。此外,查看更多代碼會更有幫助,最好是[MCVE](// stackoverflow.com/help/mcve)。一個猜測:在循環中定義了「$ event」嗎? (例如'@foreach($ events as $ event)') –

+0

是$事件來自@foreach循環 –

回答

0

使用方法如下

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

Tnx隊友!它的工作原理:D –

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

當我這樣做時出現同樣的錯誤 –

+0

@Wilfred van Eck什麼是錯誤信息?試試最近更新的版本... – Poiz

+0

ErrorException在79487d15e7f32bf64b98bc29e6d7b892a687253e.php第8行: 嘗試獲取非對象的屬性(視圖:C:\ xampp \ htdocs \ WWO_SOJ \ resources \ views \ events.blade.php) –