【发布时间】:2017-12-16 10:34:17
【问题描述】:
我试图在我的文章中呼应用户的名字,我得到了
ErrorException: Trying to get property of non-object.
我的代码:
报价模式
class Offer extends Model
{
public function countries()
{
return $this->belongsTo(Country::class, 'country_id');
}
}
国家模式
class Country extends Model
{
public function offers()
{
return $this->hasMany(Offer::class);
}
}
index.blade
{{ $offer->countries->name }}
OfferController
public function index()
{
$offers = Offer::latest()->paginate(5);
return view('admin.offers.index', compact('offers'))
当 Idd($offer->first()->countries) 时结果为空。
【问题讨论】:
-
试试 echo "
"; print_r($offers);死;
-
另外 $offer 不是一个正确的对象,你返回的是 $offers
-
您可能需要更新您的刀片文件,例如 foreach @foreach($offers as $offer) {{ $offer->countries->name }} @endforeach 但首先检查您的 json 中的内容字符串。
-
“{{ $offer->countries->name }}”在 foreach 循环中插入 print_r($offers) 的位置?
-
在foreach的顶部进行检查
标签: php laravel laravel-5 laravel-5.5