1
所以我在Laravel關係中遇到了一些問題。在Lumen/Laravel查詢關係字段5.3
我有鉛轉換一個模型,它有一個細節hasOne關係模型,詳細模型包含hashkey領域,我需要使用。
public function details()
{
return $this->hasOne('App\Models\CampaignDetails', 'n_idcampaign', 'n_idcampaign')->select(['n_idcampaign', 'hashkey']);
}
到目前爲止好,我真的只需要hashkey,但我需要返回idcampaign以及一個外鍵,否則它不會出於某種原因...
當我查詢模型時,我執行以下操作。
return $this->model->with('details')->where('n_idcampaign', 1884)->first();
到目前爲止,一切都很好,就返回了整個事情,這是一個痛苦來訪問它像first()->details->hashkey
但至少它的工作原理...
更大的問題時,我需要做的地方出現查詢基於該hashkey ...
return $this->model->with('details')->where('idData', $id)->where('hashkey', $haskkey)->first();
這是不行的,我怎樣才能讓這個我可以查詢基於一個場從關係(主模型在這種情況下hashkey)
也許試試wherePivot()而不是where? https://laravel.com/docs/5.3/eloquent-relationships – wouthoekstra
@wouthoekstra'wherePivot'用於'belongsToMany'關係。 –