【问题标题】:can't show result of two columns in blade, laravel 5.1无法显示刀片中两列的结果,laravel 5.1
【发布时间】:2015-11-24 10:23:22
【问题描述】:

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

模板:

@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:选择 * 来自information 其中information.advertise__model_id in (1, 2))

【问题讨论】:

    标签: php mysql laravel laravel-5


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 2016-10-01
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多