我试图在我的模板中显示两个表的列,但我没有成功
模板:
@foreach ($LatestSeeker_list_adv as $ads)
<span>ads id</span><span>{{ $ads->advertise->id }}</span>
<span>inf id</span><span>{{ $ads->information->id }}</span>
@endforeach
控制器:
public function SeekerLatest()
{
$LatestSeeker_list_adv = Advertise_Model::all();
return view('Users.Seeker.LatestAds.index',compact('LatestSeeker_list_adv','LatestSeeker_list_inf'));
}
广告模型:
public function Information()
{
return $this->hasOne('App\Information_Model');
}
信息模型:
public function Advertise()
{
return $this->belongsTo('App\Advertise_Model');
}
宣传迁移:
Schema::table('advertise', function($table) {
$table->foreign('information_id')->references('id')->on('information');
});
编辑:
错误:
尝试获取非对象的属性(查看: C:\用户\ Aghapesae \桌面\ baroot \资源\视图\ Users \用户导引头\ LatestAds \ Index.blade.php)
答案 0 :(得分:0)
使用with
public function SeekerLatest()
{
$LatestSeeker_list_adv = Advertise_Model::with('Information')->get();
return view('Users.Seeker.LatestAds.index',compact('LatestSeeker_list_adv','LatestSeeker_list_inf'));
}
在视图中你可以这样做:
@foreach ($LatestSeeker_list_adv as $ads)
<span>ads id</span><span>{{ $ads->id }}</span>
<span>inf id</span><span>{{ $ads->Information->id }}</span>
@endforeach