What is eloquent?
Eloquent is an ORM, all known as an Object relational Mapping, translated as a "relational mapping" (if it is only treated as the database Abstraction Layer Array Library abstraction level, it is too underestimated). The so-called "object" is the "model" in this paper, and the object relation mapping is the relationship between the models. English Document: Http://laravel-china.org/docs/eloquent#relationships
Below we begin to study one by one.
One-to-one relationship
As the name implies, this describes one-to-one relationships between two models. This relationship does not require an intermediate table.
If we have two models: User and account, which correspond to registered users and consumers, they are one-to-two relationships, then if we want to use the one-to-one relationship method provided by eloquent, the table structure should look like this:
User:id ... account_id
Account:id ... user_id
Suppose we need to query the User model for the corresponding account table information, then the code should be like this. '/app/models/user.php ':
<?php
Class User extends Eloquent {
Protected $table = ' users ';
Public Function Hasoneaccount ()
{
return $this->hasone (' account ', ' user_id ', ' id ');
}
}
Then, when we need to use this relationship, how to use it? As follows:
$account = User::find (Ten)->hasoneaccount;
The resulting ' $account ' is an instance of the ' Account ' class.
The hardest part here is the two Foreign_key and Local_key settings in the back, and you can remember this: in the User class, the second argument is ' user_id ', and the third parameter is usually ' ID ', regardless of who hasOne. Since the previous ' find (10) ' has locked the ID = 10, the corresponding SQL for this function is: ' SELECT * from account where user_id=10 '.
This code, in addition to showing a one-to-one relationship to how to use, also conveys three points of information, but also for the use of eloquent when you are recommended:
1. Specify a table name in each Model
2. Has a relationship such as "hasoneaccount ()" Rather than the simple ' account () '
3. Write full parameters each time you use the relationship between models, do not omit
Correspondingly, if you use the Belongsto () relationship, you should write this:
<?php
Class Account extends Eloquent {
Protected $table = ' accounts ';
Public Function Belongstouser ()
{
return $this->belongsto (' User ', ' user_id ', ' id ');
}
}
One-to-many relationships
Learning the basic method of using a one-to-one relationship before, the following relationships are much simpler.
We introduce a new model:pay, payment record. The table structure should look like this:
User:id ...
Pay:id ... user_id
User and pay have a one-to-many relationship, in other words a user can have multiple pay, so there is only one ' user_id ' field in the Pay table. '/app/models/user.php ':
<?php
Class User extends Eloquent {
Protected $table = ' users ';
Public Function Hasmanypays ()
{
return $this->hasmany (' pay ', ' user_id ', ' id ');
}
}
Then, when we need to use this relationship, how to use it? As follows:
$accounts = User::find (->hasmanypays) ()->get ();
The resulting ' $accounts ' is an instance of the ' Illuminate\database\eloquent\collection ' class. You should also have noticed that this is not a simple '--hasoneaccount ' but '->hasmanypays ()->get () ', why? Because this is ' hasmany ', the operation is a collection of objects.
The use of the corresponding Belongsto () is the same as the one-to-one relationship:
<?php
Class Pay extends Eloquent {
Protected $table = ' pays ';
Public Function Belongstouser ()
{
return $this->belongsto (' User ', ' user_id ', ' id ');
}
}
Many-to-many relationships
Many-to-many relationships are completely different from previous relationships, because many-to-many relationships can result in a lot of redundant data, which is not available with the tables that were brought in before.
We define two models: article and tag, each representing articles and tags, and they are many-to-many relationships. The table structure should look like this:
Article:id ...
Tag:id ...
article_tag:article_id tag_id
Used in Model:
<?php
Class Tag extends Eloquent {
Protected $table = ' tags ';
Public Function belongstomanyarticle ()
{
return $this->belongstomany (' article ', ' Article_tag ', ' tag_id ', ' article_id ');
}
}
Note that the third parameter is the ID of this class, and the fourth parameter is the ID of the class that is the first parameter.
Use the same as Hasmany:
$tagsWithArticles = Tag::take (->get) ()->belongstomanyarticle ()->get ();
Here you get a very complex object that you can ' var_dump () ' yourself. To tell you a trick, ' var_dump () ', with Chrome right-click "View Source", you can see very neat objects/arrays unfold.
Here we show you a rare usage (artifice):
Public Function Parent_video ()
{
return $this->belongstomany ($this, ' video_hierarchy ', ' video_id ', ' video_parent_id ');
}
Public Function Children_video ()
{
return $this->belongstomany ($this, ' video_hierarchy ', ' video_parent_id ', ' video_id ');
}
Yes, you can belongstomany yourself if you don't read it right.
Laravel eloquent-Model Relationship (association)