Laravel5: How are Eloquent model relationships expressed in the database?

廉价感情. 提交于 2019-12-24 04:08:09

问题


There's a missing link I fail to understand.

I use migrations to create database tables and I define the relationships there. meaning.. if I have a person table and a job table and I need a one to many relationship between the person and jobs, I'd have the job table contain a "person_id".

When I seed data or add it in my app, I do all the work of adding the records setting the *_id = values etc.

but somehow I feel Laravel has a better way of doing this.

if I define that one to many relationship with the oneToMany Laravel Eloquent suports:

in my Person model.....

 public function jobs()
    {
        return $this->hasMany('Jobs);
    }

what's done on the database level? how do I create the migration for such table? Is Laravel automagically doing the "expected" thing here? like looking for a Jobs table, and having a "person_id" there?


回答1:


Yep, Laravel is doing what you guess in your last paragraph.

From the Laravel documentation for Eloquent Relationships (with the relevant paragraph in bold):

For example, a User model might have one Phone. We can define this relation in Eloquent:

class User extends Model {

    public function phone()
    {
        return $this->hasOne('App\Phone');
    }

}

The first argument passed to the hasOne method is the name of the related model. Once the relationship is defined, we may retrieve it using Eloquent's dynamic properties:

$phone = User::find(1)->phone;

The SQL performed by this statement will be as follows:

select * from users where id = 1

select * from phones where user_id = 1

Take note that Eloquent assumes the foreign key of the relationship based on the model name. In this case, Phone model is assumed to use a user_id foreign key.

Also note that you don't actually have to explicitly set the foreign key indexes in your database (just having those "foreign key" columns with the same data type as the parent key columns is enough for Laravel to accept the relationship), although you should probably have those indexes for the sake of database integrity.




回答2:


There is indeed support to create foreign key relationships inside migration blueprints and it's very simple too.

Here is a simple example migration where we define a jobs table that has a user_id column that references the id column on users table.

Schema::create('jobs', function($table)
{
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');

});

You can also use some other methods that laravel provides such as onDelete() or onUpdate

Of course to understand better the options that are available to you please read the documentation here.

Edit:

Keep in mind that Eloquent is just using fluent SQL wrapper and behind the scenes there are just raw sql queries, nothing magical is happening, fluent just makes your life a lot easier and helpers you write maintainable code. Take a look here about the Query Builder and how it works and also, as @Martin Charchar stated , here about Eloquent and relationships.



来源:https://stackoverflow.com/questions/30388426/laravel5-how-are-eloquent-model-relationships-expressed-in-the-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!