Data tables are often associated with each other. For example, a blog post might have multiple comments, or an order might correspond to a single customer. Eloquent makes it easy to manage and process these associations, and it also supports multiple types of associations:
One
One-to-a-correlation is a very basic association. For example, a User model might correspond to a Phone. To define this association, we must place the phone method on the User model. The phone method should return the result of the Hasone method on the base class eloquent:
HasOne (' App\phone ');} }
The first parameter passed to the HasOne method is the class name of the associated model. Once the association is defined, we can use the dynamic properties of eloquent to get the associated record. Dynamic properties allow you to access the associated functions as if they were attributes defined in the model:
$phone = User::find (1)->phone;
Eloquent assumes that the corresponding associated foreign key name is based on the model name. In this example, it automatically assumes that the phone model has a user_id foreign key. If you want to override this convention, you can pass in the second argument to the HasOne method.
return $this->hasone (' App\phone ', ' Foreign_key ');
In addition, the default foreign key for eloquent has a corresponding value in the ID field of the upper model. In other words, eloquent will look for a record of the user's ID field with the same value as the user_id field of the Phone model. If you want the association to use a value other than the ID, you can pass the third argument to the HasOne method to specify your own custom key:
return $this->hasone (' App\phone ', ' foreign_key ', ' Local_key ');
View official documents
Project Combat:
Create a student score table grades, and establish a foreign key user_id associated with the user table ID:
Because a user can only correspond to a single score table, this is a one-to-two model, and then add a score association in the user model
' Required|digits:10 ', ' password ' = ' required ']; }/** * Establish a one-to-one relationship */Public function grade () {return $this->hasone (' App\grade '); }}
According to a login user, and then get the results of the Landing User (union query):
/** * Back to Student home */public function Home () { $grade = Auth::user ()->grade; Return view (' Stu.home ', compact (' Grade '));}
But here is an error :
Why did you get an error?
After analyzing and querying the official documents, eloquent will assume that the corresponding foreign key name is based on the model name. In this example, it automatically assumes that the grade model has a users_info_id foreign key. If you want to override this convention, you can pass in the second argument to the HasOne method.
/** * Establish a one-to-one relationship */public function grade () { //Grade table's foreign key is user_id return $this->hasone (' App\grade ', ' user_id ');}
View results:
In addition, the default foreign key for eloquent has a corresponding value in the ID field of the upper model. In other words, eloquent will look for the user's ID field with the same record as the value of the user_id field of the Grade model. If you want the association to use a value other than the ID, you can pass the third argument to the HasOne method to specify your own custom key:
This assumes that the user table's email and Grade table user_id are associated fields return $this->hasone (' App\grade ', ' user_id ', ' email ');