我试图在我的模板中显示两个表的列,但我没有成功
模板:
@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 = Advertise_Model::with('information')->get();
return view('Users.Seeker.LatestAds.index',compact('LatestSeeker_list'));
}
广告模型:
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');
});
错误:
SQLSTATE [42S22]:找不到列:1054未知列 'where子句'中的'information.advertise__model_id'(SQL:select * 来自
information
information
。advertise__model_id
in(1, 2))
答案 0 :(得分:1)
您需要定义关系列,以便laravel知道需要选择哪些行来显示关系。
public function Information()
{
return $this->hasOne('App\Information_Model', 'id', 'information_id');
}
有关详细信息,请参阅http://laravel.com/docs/5.1/eloquent-relationships#one-to-one