laravel一对一的关系 [英] laravel one to one relationship

查看:450
本文介绍了laravel一对一的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 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 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 the general_information table.

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

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.

I tried to replace associate with attach but I got that attach 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 the primary key of parent table in the child table as a 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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆