Reputation: 231
I am trying to show columns of two tables in my template, but I have not been successful
Template :
@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
Controller :
public function SeekerLatest()
{
$LatestSeeker_list = Advertise_Model::with('information')->get();
return view('Users.Seeker.LatestAds.index',compact('LatestSeeker_list'));
}
Advertise Model :
public function Information()
{
return $this->hasOne('App\Information_Model');
}
Information Model :
public function Advertise()
{
return $this->belongsTo('App\Advertise_Model');
}
advertise migration :
Schema::table('advertise', function($table) {
$table->foreign('information_id')->references('id')->on('information');
});
error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'information.advertise__model_id' in 'where clause' (SQL: select * from
information
whereinformation
.advertise__model_id
in (1, 2))
Upvotes: 0
Views: 262
Reputation: 1288
You need to define the relationship columns so that laravel knows what rows it needs to to select to display the relationship.
public function Information()
{
return $this->hasOne('App\Information_Model', 'id', 'information_id');
}
See http://laravel.com/docs/5.1/eloquent-relationships#one-to-one for more information
Upvotes: 1