所以這裏的問題,當我這樣做:試圖讓非對象錯誤的財產,我喜歡
<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');
}
}
我打算繼續,猜測$ event-> media是JSON字符串而不是對象。什麼'print_r($ event-> media)'說? – apokryfos
歡迎來到Stack Overflow!爲了讓你的問題更容易閱讀,請[格式化你的代碼和錯誤輸出](// stackoverflow.com/help/formatting),特別是每個代碼/輸出行前加4個空格。此外,查看更多代碼會更有幫助,最好是[MCVE](// stackoverflow.com/help/mcve)。一個猜測:在循環中定義了「$ event」嗎? (例如'@foreach($ events as $ event)') –
是$事件來自@foreach循環 –