简体   繁体   中英

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:

    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. Model for Table 1 has:

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?

b.) How do I set the relationship in my code? 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

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.

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...?

c.) Is there any point in this line in the migration: $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?

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.

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. 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 is considered a child of 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:

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

If you're updating a Table1 record, you can use 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 . 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. 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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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