Processing of multi-table relationships in the PhpLaravel framework: Eloquent one-to-multiple relationship processing Php Laravel framework multi-table relationship processing-Eloquent one-to-multiple relationship processing
This blog article mainly introduces how to process one-to-multiple relationships with Eloquent in the Laravel framework)
Your database may be related to each other. For example, a blog post may have many comments or an order is related to the user who placed the order. Eloquent makes it easy to manage and process these relationships. Laravel provides four types of relationships:-one-to-many-Multi-polymorphism.
One-to-multiple
An example of a one-to-many relationship is a blog article with many comments or scores of a course. We can define a relational Model like this:
HasMany ('scoreinfo', 'soc _ id') ;}}?>
Define the corresponding inverse relationship Model:
Belonsto ('stuinfo','s _ id');}/** define a reverse link pointing to the primary key table **/public function SobjectInfo () {return $ this-> belonsto ('sobjectinfo ', 'soc _ id') ;}}?>
The one-to-many relationship between tables has been established through the above steps,
The following describes the implementation of drop-down list query, binding, and other applications in the Laravel Administrato background.
'Score info', // topic name 'single '=>', // Create a description 'model' => 'scoreinfo ', // score information 'form _ width' => 960, // column width on the left // list 'column' => 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 ', 'select' => '(: table ). soc_nam E ',), 'Score' => array ('title' => 'test scores ', '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 scores', 'type' => 'number '),), // modify and add 'edit _ fields '=> ar Ray ('stuinfo' => array ('type' => 'relationship ', 'title' => 'student name ', 'name _ field' =>'s _ name',), 'sobjectinfo '=> array ('type' => 'relationship ', 'title' => 'course name', 'Name _ field' => 'soc _ name ',), 'score '=> array ('title' => 'test scores', 'type' => 'text'),);?>
The preceding example shows the background score information class.
In this example, "student name" and "course name" are used multiple times. although they are stored in different tables, we have established a one-to-multiple relationship between them in the Model, therefore, we can freely combine
As follows: