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:

<?php/** * Sobjectinfo: Course Information Table Model * soc_id     : primary key self-increment * soc_name   : Course Name * Soc_teacher: Instructor **/class Sobjectinfo Exten DS Eloquent {    //Custom table name (protected $table)    protected $table = ' sobjectinfo ';    Custom primary key (protected $primaryKey)    protected $primaryKey = ' soc_id ';    Turn off automatic maintenance of creation time and update time (protected $timestamps) public    $timestamps = false;    /     * * Define a one-to-many relationship */public    function Scoreinfo () {        return $this-hasmany (' Scoreinfo ', ' soc_id ');}    } ?>
Define the corresponding inverse relationship Model:
<?php/** * Scoreinfo: Score Information Table Model * so_id   : PRIMARY KEY auto-increment * s_id    : Student Information table (STUINFO) Primary key *  soc_id: Course Information table (SOBJECTINFO) primary key * s Core   : Score */class Scoreinfo extends eloquent {       //Custom table name (protected $table)       protected $table = ' scoreinfo ';       Custom primary key (protected $primaryKey)       protected $primaryKey = ' so_id ';       Turn off automatic maintenance of creation time and update time (protected $timestamps) public       $timestamps = false;       /        * * Score list (Scoreinfo) has primary foreign key relationship with curriculum (SOBJECTINFO), Student Information table (STUINFO)        * and is a one-to-many relationship */public        function Stuinfo () {            return $this-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
<?phpreturn Array (' title ' = ' score info ',//Column name ' single ' = ' >> ',//New description ' model ' =& Gt ' Scoreinfo ',//score information ' form_width ' + 960,//Left column width//list ' columns ' = = Array (' so_id ' =&G T        Array (' title ' = ' = ' number ', ' select ' = ' so_id ', ' sort_field ' = ' so_id '), ' S_name ' =>array (' title ' = ' Student name ', ' relationship ' = ' stuinfo ', ' select ' =&gt ; ' (: Table). S_name ', '), ' Soc_name ' =>array (' title ' = ' Course name ', ' relationship ' = ' s ') Objectinfo ', ' select ' + ' (: table). Soc_name ', '), ' score ' =>array (' title ' = ' = ' Exam            Fractions ', ' select ' = ' score '),),//filter Information ' filters ' = = Array (' so_id ' = = Array ( ' title ' = ' = ' number '), ' Sobjectinfo ' =>array (' type ' = ' relationship ', ' Ti Tle ' = ' &Lt;span style= "font-family:arial, Helvetica, Sans-serif;" > Course name </span><span style= "Font-family:arial, Helvetica, Sans-serif;" > ',</span> ' name_field ' = ' soc_name ', ' Stuinfo ' =>array (' type ' =&G T ' Relationship ', ' title ' = ' Student name ', ' name_field ' = ' s_name ', ' score ' =>arr Ay (' title ' = ' Test score ', ' type ' = ' number '),//Modify, add ' edit_fields ' = Arra Y (' stuinfo ' =>array (' type ' = ' relationship ', ' title ' = ' Student name ', ' name_ Field ' = ' s_name ',), ' Sobjectinfo ' =>array (' type ' = ' relationship ', ' t Itle ' + ' course name ', ' name_field ' = ' soc_name ', ' Score ' =>array (' title ' = ' + ') ' Test score ', ' type ' = ' 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:



10 Laravel4 developer must use expansion pack: http://blog.csdn.net/yimiyuangguang/article/details/39756115
Laravel Administrator Documentation

Http://administrator.frozennode.com/docs/field-type-relationship

Laravel4 Chinese help Manual:

Http://pan.baidu.com/s/1jGl6cqa

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

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.