简体   繁体   English

如何在Laravel(5.4)Eloquent中定义单向一对一关系?

[英]How to define a one-way one-to-one relationship in Laravel (5.4) Eloquent?

Think I'm missing something obvious here, but I want to define a one way, one to one relationship from table_1 to table_2, eg table1 schema: 想想我这里缺少明显的东西,但是我想定义一种从表_1到表_2的一对一关系,例如表1模式:

    Schema::create('table1', function (Blueprint $table) {
        // Some field definitions
        $table->integer('table2_id')->unsigned();
        $table->foreign('table2_id')->references('id')->on('table2')->onDelete('cascade');
    });

Table 2 doesn't know anything about Table 1, so just has a bunch of fields defined. 表2对表1一无所知,因此只定义了一堆字段。 Model for Table 1 has: 表1的模型具有:

public function table2() // Get table2 record
    {
        return $this->hasOne('App\Table2');
    }

Questions: 问题:

a.) Is that relationship in the record necessary just to be able to lookup the relevant table2 record from a table1 record? a。)记录中的关系是否仅需要能够从table1记录中查找相关的table2记录?

b.) How do I set the relationship in my code? b。)如何在代码中设置关系? Currently my controller code is: 目前,我的控制器代码是:

$table_1_record = new Table1();
// What code here to define the relationship, using Eloquent? Or do I just do:
$table_1_record->table2_id = my_table2_record->id;
// But this just sets it manually doesn't it, rather than using Eloquent?

Thing that is confusing me is in here: https://laravel.com/docs/5.6/eloquent-relationships#one-to-one a bit further down from the link, where it says 让我感到困惑的是在这里: https : //laravel.com/docs/5.6/eloquent-relationships#one-to-one距离链接较远,它说

Eloquent determines the foreign key of the relationship based on the model name. 雄辩地基于模型名称确定关系的外键。 In this case, the Phone model is automatically assumed to have a user_id foreign key. 在这种情况下,将自动假定Phone模型具有user_id外键。

Applying that example to my code, the thing is I don't want to have to define a table1_id in my Table_2 - but sounds from that quote from the docs that I need to, to find a table2 record from table1...? 将该示例应用于我的代码,事情是我不想在我的Table_2中定义table1_id-但听起来像是我需要从文档中引用的内容,以便从table1中找到table2记录...?

c.) Is there any point in this line in the migration: $table->foreign('table2_id')->references('id')->on('table2')->onDelete('cascade'); c。)迁移中的这一行是否有任何意义: $table->foreign('table2_id')->references('id')->on('table2')->onDelete('cascade'); and indeed using Eloquent at all (I want to do things the proper Laravel way), or shall I just simply manually set table2_id as in question b.) above? 并确实使用了Eloquent(我想以正确的Laravel方式做事),还是我应该像问题b那样简单地手动设置table2_id)?

Note: I don't want to define the inverse of the relationship, as I don't need to find a table_1 record from a table_2 record. 注意:我不想定义关系的倒数,因为我不需要从table_2记录中找到table_1记录。

Thanks for any help. 谢谢你的帮助。

Relationships in Eloquent only work in the directions that they are defined. 雄辩的关系仅在定义的方向上起作用。 If you want to be able to find Table2 based on the foreign key defined in Table1 , you would add a relationship to the Table1 model only. 如果希望能够基于Table1定义的外键查找Table2 ,则可以仅向Table1模型添加关系。 You do not need to define relationships on both models in order to go one direction. 您无需为两个方向定义两个模型上的关系。

Given your table1 schema, you are actually using the inverse of a one-to-one relationship. 给定您的table1模式,您实际上正在使用一对一关系的逆函数 table1 is considered a child of table2 . table1被视为table2的子级。 Your relationship would look like this: 您的关系如下所示:

class Table1
{
    public function table2()
    {
        return $this->belongsTo(\App\Table2::class);
    }
}

As for setting the relationship, my recommendation is to simply apply the ID from table2 manually when creating the initial record: 至于设置关系,我的建议是在创建初始记录时简单地手动应用table2的ID:

`$table1->table2_id = $table2->id;`

If you're updating a Table1 record, you can use associate() . 如果要更新Table1记录,则可以使用associate() More info on that here in the docs. 有关更多信息,请参见文档。 Note that this does require you to define the other side of the relationship on Table2 . 请注意,这确实需要您在Table2上定义关系的另一端。 If you don't want to do that, just update the column manually when updating also. 如果您不想这样做,也可以在更新时手动更新列。

Keep in mind that all of this is powered by Eloquent. 请记住,所有这些都是由Eloquent提供支持的。 Just because you're defining a column value manually instead of using a relationship helper method doesn't mean you're doing something ineffectively or incorrectly. 仅仅因为您手动定义列值而不是使用关系帮助器方法并不意味着您在做无效或不正确的事情。

Is there any point in [the foreign key] line in the migration 迁移中[外键]行中是否有任何意义

This creates a foreign key constraint in the database software itself, and has nothing to do with Eloquent. 这会在数据库软件本身中创建外键约束,与Eloquent无关。 Eloquent doesn't use or care if you have a foreign key defined. 口才不使用或不在乎您是否定义了外键。 There are benefits to using foreign keys, though, and suggest you look into it further to determine if they're a good idea for your application. 但是,使用外键有很多好处,建议您进一步研究一下外键,以确定它们是否适合您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM