[英]How to show two table columns in laravel blade
我试图在模板中显示两个表的列,但未成功
范本:
@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:\\ Users \\ Aghapesae \\ Desktop \\ baroot \\ resources \\ views \\ Users \\ Seeker \\ LatestAds \\ Index.blade.php)
使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.