无法显示刀片中的两列结果,laravel 5.1

时间:2015-11-24 10:23:23

标签: php mysql laravel laravel-5

我试图在我的模板中显示两个表的列,但我没有成功

模板:

@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 informationadvertise__model_id in(1,   2))

1 个答案:

答案 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