Laravel supports multiple models of relation, corresponding to One2One, one2many,many2many,hasmanythrough,polymorphic, many2many polymorphic relationships between models.
Heart
1. All relation are defined by the method on the model class;
2. Relationship and model itself are based on query Builder, so the operation of relation can also use a method similar to query Builder, such as: can support cascading;
3.Dynamic property of Model: This is the result of the Model->relation method reference, which returns the value of the relation model directly, does not support the Cascade operation of Query Builder, This is the essential difference from the Model->relation () method invocation.
Which means model->relation = model->relation ()->get ()
Types of relationships:
One 2 One (one-to-one)
Example user HasOne phone, phone belongsto user this relationship. To make this relationship work, the following prerequisites are required:
1. User model
Base Table:users,
Field presets: ID
Relation Method:phone () {return $this->hasone (' App\phone ')}
Note: According to the naming conventions of Laravel, Laravel assumes that there is a user_id field in the phones table as the foreign key of the ID in the Users table.
We can override this specification by passing in another parameter in the user () method, such as $this->hasone (' App\phone ', ' owner_id ')
Here owner_id is the field name of the phones table that references the ID of the users table (default is user_id);
2.Phone Models
Base Table:phones,
Field Presets: user_id
Relation Method:user () {return $this->belongsto (' App\user ')}
Here, Laravel also presets an ID field in the parent table (that is, the users table) primary key, if your parent table does not use an ID as the primary key, in the above Hasone call, you can use the third parameter to specify the primary key, such as
User () {return $this->hasone (' App\user ', ' owner_id ', ' u_id '}. Here owner_id the foreign key reference field name for the Users table primary key (phones) in the u_id table
One 2 Many (one-to-many)
Example: Post Hasmany Comments, Comment Belongsto Post, this is a typical one-to-many relationship model
1. Post model
Base Table:posts
Field presets: ID indicates row primary key
Relation method:comments () {return $this->hasmany (' App\comment ')}
If you need to override the default naming conventions, we need to call this:
Public Function Comments () {return $this->hasmany (' app\comment ', ' foreign_owner_postid_key ', ' Local_postid_key ')}
Note here: Foreign_owner_postid_key is the name of the field to be in the comments table;
Local_postid_key is posts the primary key field name that must be in this table
2. Comment Model
Base Table:comments
Field presets: post_id as foreign key to posts table
Relation Method:post () {return $this->belongsto (' App\post ')}
If you need to override the default naming conventions, we also need to call this:
Public Function post () {return $this->belongsto (' app\post ', ' foreign_owner_postid_key ', ' Other_local_postid_key '}
Note: Here Foreign_owner_postid_key is the foreign key field name of the reference posts table that must be in this comments table;
Other_local_postid_key is the name of the primary key that must be in the posts table. The reason for adding local,other is that it represents the table or the associated table.
In addition, it has been mentioned that because relationship itself is Query Builder, you can increase the constraint cascade, for example:
Post::find (1)->comments ()->where (' title ', ' foo ')->first () only takes the first post corresponding to the title Foo comment
Other_local_postid_key is
Note that according to laravel default naming conventions, a one-to-many relationship model, the owning model in the child model
Laravel Model Relationship