[英]laravel one to one relationship
I have xml_document
table with these columns: 我有带有这些列的xml_document
表:
id
general_information_id
I have general_information
table with these columns: 我有这些列的general_information
表:
id
domain
log_file
The relationship between them is one to one. 它们之间的关系是一对一的。
Have I build the relationship correctly? 我是否正确建立了关系? Or I need to add xml_document_id
column to the general_information
table. 或者我需要将xml_document_id
列添加到general_information
表中。
Secondly: 其次:
I have added a row to the xml_doucment
and now I want to add a row to the general_information
and link this new row to the xml_document
我已经添加一行到xml_doucment
,现在我想将行添加到general_information
这个新行链接到xml_document
I tried this: 我尝试了这个:
$xmlDocument = XmlDocument::find(Input::get(4));
$generalInformation = new GeneralInformation($dataGeneralInformation);
$generalInformation->xmlDocument()->associate($xmlDocument);
//$xmlDocument->generalInformation()->attach($generalInformation);
$generalInformation->save();
$xmlDocument->save();
but I got error that xml_document_id
column doesn't exist in the general_information
table. 但我得到了错误xml_document_id
列不中存在general_information
表。
I tried to replace associate
with attach
but I got that attach
is not an existed function. 我尝试用attach
替换associate
,但是我发现attach
不存在。
please help me I am tried of this one to one relationship, I couldn't know what is the correct way to do id. 请帮助我,我已经尝试过这种一对一的关系,我不知道做id的正确方法是什么。 I don't know where to add columns in the database and what to do in the models. 我不知道在数据库中的哪里添加列以及在模型中要做什么。 I have tried a lot of things but still so confused. 我尝试了很多事情,但仍然感到困惑。
class GeneralInformation extends Eloquent{
.....
public function xmlDocument(){
return $this->belongsTo('XmlDocument');
}
}
class XmlDocument extends Eloquent {
....
public function generalInformation(){
return $this->hasOne('GeneralInformation','general_information_id');
}
}
To make a one-to-one
relationship, you need to store the primary key
of parent table in the child table as a foreign key
. 要建立one-to-one
关系,您需要将父表的primary key
作为foreign key
存储在子表中。 So if xml_document
is parent and if it contains many general_information
then the id
field of the xml_document
should be present in in the general_information
table as xml_document_id
. 所以,如果xml_document
是父母,如果它包含许多general_information
则id
基于场xml_document
应该是存在于general_information
表作为xml_document_id
。
So, you may build the one-to-one
relation like this: 因此,您可以像这样建立one-to-one
关系:
// xml_document model
public function generalInfo()
{
return $this->hasOne('GeneralInformation');
}
Then the declare the GeneralInformation
model. 然后声明GeneralInformation
模型。
This is what you want: 这就是你想要的:
class GeneralInformation extends Eloquent
{
public function xmlDocument()
{
return $this->hasOne('XmlDocument');
}
}
class XmlDocument extends Eloquent
{
public function generalInformation()
{
return $this->belongsTo('GeneralInformation','general_information_id');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.