2017-09-11 23 views
0

我正在使用一对一的关系。我的模型代码是:Laravel与数组值的关系

public function training_type() 
{ 
    return $this->hasone('App\Training_type','id','type'); 
} 

但参数“ID”是一个数组中,我试着用下面的代码,但不工作:

public function training_type() 
{ 
    return $this->hasone('App\Training_type','types.id','type'); 
} 

任何人给我很好的suggession。

Training_type表结构,

"company_id": "company1", 
"types": [ 
{ 
"id": "1", 
"name": "Generic" 
}, 
{ 
"id": "2", 
"name": "Compensation and Benefits" 
}, 
{ 
"id": "3", 
"name": "Labor and Employment Law" 
} 
], 
"updated_at": ISODate("2017-08-10T10:24:40.000+05:30") 
} 

另一个表结构,

{ 
"company_id": "company1", 
"title": "Fire Extinguisher\t", 
"content_type": "", 
"links": [ 
{ 
"link": "" 
}, 
{ 
"link": "" 
} 
], 
"updated_at": ISODate("2017-08-10T10:24:40.000+05:30"), 
"type": "1" 
} 
+0

其中'types.id'来自哪里? – Gammer

+0

更新了问题 –

+0

您不应该像正在做的那样传递foreign_key,Foreign_key是您引用的表的直接向前列名称。看下面的例子。 – Gammer

回答

0

传递foreign_key到关系的通常的做法是:

public function training_type() 
{ 
    return $this->hasone('App\Training_type','foreign_key'); 
} 

的foreign_key是从你想链接的表格。