Laravel5 student score management system-04-Eloquent Association

Source: Internet
Author: User
Laravel5 student score management system-04-Eloquent associated data tables are often associated with each other. For example, a blog post may have multiple comments, or an order may correspond to an order customer. Eloquent makes it easy to manage and process these associations, and supports multiple types of associations:

One to One #

One-to-one association is a basic Association. For example, a User model may 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. After the association is defined, we can use the dynamic attribute of Eloquent to obtain the association record. Dynamic attributes allow you to access associated functions, just as they are defined in the model:

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

Eloquent assumes that the associated foreign key name is based on the model name. In this example, it automatically assumes that the Phone model has the user_id foreign key. If you want to override this convention, you can pass in the second parameter to the hasOne method.

return $this->hasOne('App\Phone', 'foreign_key');

In addition, the default foreign key of Eloquent has a corresponding value in the id field of the upper-layer model. In other words, Eloquent searches for records with the same user id field value as the user_id field value of the Phone model. If you want to associate a value other than id, you can pass the third parameter to the hasOne method to specify your custom key:

return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

View official documents


Project practice:

Create the student's notebook table grades and associate the foreign key user_id with the user table id:


Because a user can only correspond to one score table, this is a one-to-one model, and then adds the SCORE Association to the user model.

          
 'Required | digits: 10', 'password' => 'requestred'];}/*** create a one-to-one relationship */public function grade () {return $ this-> hasOne ('app \ grad ');}}


Based on a login user, and then obtain the score of the login user (joint query ):

/*** Return to student homepage */public function home () {$ grade = Auth: user ()-> grade; return view ('Stu. home ', compact ('Grad '));}


But here we willError:

Why is an error reported?

After analysis, query the official documentation. the Eloquent assumes that the associated foreign key name is based on the model name. In this example, it automatically assumes that the Grade model has a foreign key users_info_id. If you want to override this convention, you can pass in the second parameter to the hasOne method.

/*** Create a one-to-one relationship */public function grade () {// the foreign key of the grade table is user_id return $ this-> hasOne ('app \ grad ', 'user _ id ');}


View Score:

In addition, the default foreign key of Eloquent has a corresponding value in the id field of the upper-layer model. In other words, Eloquent searches for records with the same user id field value as the user_id field value of the Grade model. If you want to associate a value other than id, you can pass the third parameter to the hasOne method to specify your custom key:

// Assume that the email in the user table and the user_id in the grade table are the associated fields return $ this-> hasOne ('app \ grad', 'User _ id ', 'email ');
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.