[英]Hasone returns null in laravel 5.4
我處於交易模式:
public function order()
{
return $this->hasOne('Model\Interaction\Order');
}
我想通過交易獲得訂單,
Transaction::where('id', 1)->with('order')->get();
但訂單為空。 在transactions
表中,我有order_id
。
我究竟做錯了什么?
謝謝
如果您定義這樣的關系,則在orders
表中應該有transaction_id
。
由於您在transactions
表中有order_id
,因此關系應為belongsTo()
:
public function order()
{
return $this->belongsTo('Model\Interaction\Order');
}
由於order_id位於交互表上,因此應使用belongsTo而不是hasOne
在我的情況下,問題在於子表(即我認為具有一個關系的表)實際上具有多個具有相同值的條目。 Eloquent不會為您選擇具有匹配ID和slice 1的所有關系。 所以,如果你說
public function foo ()
return $this->hasOne(Bar::class, 'this_class_id');
}
確保Bar
的表列this_class_id
確實是唯一的,否則您將獲得空結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.