第一次嘗試laravel雄辯relatioinstipLaravel雄辯:屬於關聯關係 - 錯誤:試圖讓非對象的屬性
我知道這是很簡單,但我得到這個錯誤不知道是什麼地方錯了
我有2個表中的數據的基礎上,新聞和news_image
在數據庫
表:
news
id | header | details
news_image
id | image | news_id
並有2種型號新聞,newsImage
newsImage型號:
class newsImage extends Eloquant {
protected $table = 'news_image';
public function news()
{
return $this->belongsTo('News');
}
}
新聞模型
class News extends Eloquent
{
protected $table = 'news';
public $timestamps = false;
public function image()
{
return $this->hasMany('newsImage');
}
}
的觀點:
foreach($news as $new)
<tr>
<td> {{$new->id}} </td>
<td> {{ $new->header}}</td>
<td> {{ $new->details }}</td>
</td> {{$new->news->image}}</td>
</tr>
當我運行這個它得到錯誤:
Trying to get property of non-object (View: /var/www/html/clinics/app/views/news/index.blade.php)
上什麼可能會造成這個錯誤的任何想法?
你也可以發佈你的新聞模型嗎? – BakerStreetSystems
已更新併發布了新聞模型 – Muhammed
假設這是複製/粘貼的代碼,那麼您的'newsImage'類正在擴展的'Eloquant'就沒有這樣的東西。但我假設你沒有從中得到一個錯誤,這不是複製/粘貼? – Samsquanch