Processing of multi-table relationships in the PhpLaravel framework

Source: Internet
Author: User
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:

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.