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 ':
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:
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 ':
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:
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:
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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.