PHP laravel Framework Multi-table relational processing eloquent one-to-many relationship processing

Source: Internet
Author: User
PHP laravel Framework Multi-table relational processing eloquent one-to-many relationship processing

This post mainly introduces the processing of eloquent to one-to-many relationships in the Laravel framework, in Laravel Administrator (background expansion pack)

Your database may be related to each other. For example, a blog post may have many comments, or an order is related to the person who placed the order. Eloquent makes it easy to manage and handle these relationships. Laravel offers four types of relationships:-one-to-many-many-to-many-polymorphic relationships

One-to-many

An example of a one-to-many relationship is a blog post that has many comments or multiple score information for a course. We can define a relational model like this:

 
  Hasmany (' Scoreinfo ', ' soc_id ');}    ? >

Define the corresponding inverse relationship Model:

 
  Belongsto (' Stuinfo ', ' s_id ');        }         /* * Define the inverse relationship to the primary key table * */public        function Sobjectinfo () {            return $this-belongsto (' Sobjectinfo ', ' soc_ Id ');}        } ?>
With the above steps, a one-to-many relationship between tables and tables has been established,
The following will be introduced in the Laravel Administrato background in the implementation of the drop-down list query, binding and other applications
  ' Score information ',//Column name ' single ' = ' >> ',//New description ' model ' = ' scoreinfo ',//score info ' form  _width ' + 960,//Left column width//list ' columns ' = = Array (' so_id ' = = Array (' title ' = = =            ' Number ', ' select ' = ' so_id ', ' sort_field ' = ' so_id '), ' S_name ' =>array (       ' title ' = ' Student name ', ' relationship ' = ' stuinfo ', ' select ' = ' (: table). S_name ', ' Soc_name ' =>array (' title ' = ' Course name ', ' relationship ' = ' sobjectinfo ', ' Sele CT ' = ' (: table). Soc_name ', ' Score ' =>array (' title ' = ' Test score ', ' select ' =        ' Score '),),//filter Information ' filters ' = = Array (' so_id ' = = Array (' title ' = ' = ' number ') ), ' Sobjectinfo ' =>array (' type ' = ' relationship ', ' title ' = 'Course Name',' Name_Field ' = ' soc_name ', ' Stuinfo ' =>array (' type ' = ' relationship ', ' Title ' + ' student name ', ' name_field ' = ' s_name ', ' Score ' =>array (' title ' = ') ' Test score ', ' type ' = ' number '),//Modify, add ' edit_fields ' = = Array (' Stuinfo ' =>arra        Y (' type ' = ' relationship ', ' title ' = ' Student name ', ' name_field ' = ' s_name ',            ), ' Sobjectinfo ' =>array (' type ' = ' relationship ', ' title ' = ' Course name ', ' Name_Field ' = ' soc_name ', ' Score ' =>array (' title ' = ' Test score ', ' type ' =&G t; ' Text '),));? >
The above example shows a background score information class.

The example is used more than once to "student name", "Course name", although they are stored in different tables, but since we have established a one-to-many relationship between them in model, we are free to mix

As follows:

  • 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.