laravel一对一的关系 [英] laravel one to one relationship
问题描述
我有 xml_document
表包含这些列:
id
general_information_id
我有 general_information
具有以下列:
id
domain
log_file
它们之间的关系是一对一的。
我是否正确建立关系?或者,我需要向
general_information
表中添加xml_document_id
列。
其次:
我向
xml_doucment
添加了一行,现在我想添加一行general_information
并将此新行链接到xml_document
我试过这个:
$ xmlDocument = XmlDocument :: find(Input :: get(4));
$ generalInformation = new GeneralInformation($ dataGeneralInformation);
$ generalInformation-> xmlDocument() - > associate($ xmlDocument);
// $ xmlDocument-> generalInformation() - > attach($ generalInformation);
$ generalInformation-> save();
$ xmlDocument-> save();
但我错误地显示
xml_document_id
不存在于general_information
表中。
我试图替换
associate
与attach
但是我得到attach
不是一个存在的函数。
请帮我我试过这个一对一的关系,我不知道是什么是正确的方式做id。我不知道在数据库中添加列以及在模型中做什么。我尝试了很多事情,但仍然这么困惑。
更新1
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');
}
}
解决方案make a
one-to-one
关系,你需要将子表中的父表的主键
存储为a外键
。因此,如果xml_document
是父代,如果它包含许多general_information
,则id $ c
xml_document
中的<$ c>字段应该存在于general_information
表中xml_document_id
。
所以,你可以这样构建
一对一
关系:// xml_document模型
public function generalInfo()
{
return $ this- > hasOne('GeneralInformation');
}
然后声明
GeneralInformation
模型。I have
xml_document
table with these columns:id general_information_id
I have
general_information
table with these columns: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 thegeneral_information
table.Secondly:
I have added a row to the
xml_doucment
and now I want to add a row to thegeneral_information
and link this new row to thexml_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 thegeneral_information
table.I tried to replace
associate
withattach
but I got thatattach
is not an existed function.please help me I am tried of this one to one relationship, I couldn't know what is the correct way to do 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.
Update 1
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 theprimary key
of parent table in the child table as aforeign key
. So ifxml_document
is parent and if it contains manygeneral_information
then theid
field of thexml_document
should be present in in thegeneral_information
table asxml_document_id
.So, you may build the
one-to-one
relation like this:// xml_document model public function generalInfo() { return $this->hasOne('GeneralInformation'); }
Then the declare the
GeneralInformation
model.这篇关于laravel一对一的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!