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: