Table of Contents
      一对多
Home Backend Development PHP Tutorial Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理

Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理

Jun 23, 2016 pm 01:47 PM

Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理

        本博文主要介绍 Laravel 框架中 Eloquent  对一对多关系的处理,在 Laravel Administrator(后台扩展包)

        您的数据库可能是彼此相关的。比如,一篇博客文章可能有许多评论,或者一个订单与下订单的用户相关。Eloquent 使得管理和处理这些关系变得简单。Laravel 提供了四种类型的关系: - 一对一 - 一对多 - 多对多 - 多态关系

      一对多

一个一对多关系的例子是一篇博客文章有许多评论或者一个课程有的多次分数信息等。我们可以像这样定义关系模型 Model:

<?php /** * sobjectinfo:课程信息表 Model * soc_id     :主键自增 * soc_name   :课程名 * soc_teacher:授课老师 **/class SobjectInfo extends Eloquent {    //自定义表名(protected $table)    protected $table = 'sobjectinfo';    //自定义主键(protected $primaryKey)    protected $primaryKey = 'soc_id';    //关闭 创建时间 与 更新时间 的自动维护(protected $timestamps)    public $timestamps = false;    /*     * 定义一对多关系     */    public function Scoreinfo(){        return $this -> hasMany('Scoreinfo','soc_id');    }}?&gt;
Copy after login

定义与之对应的逆向关系 Model:

<?php /** * scoreinfo:分数信息表 Model * so_id   :主键自增 * s_id    :学生信息表(stuinfo)主键 * soc_id  :课程信息表(sobjectinfo)主键 * score   :分数 */class ScoreInfo extends Eloquent {       //自定义表名(protected $table)       protected $table = 'scoreinfo';       //自定义主键(protected $primaryKey)       protected $primaryKey = 'so_id';       //关闭 创建时间 与 更新时间 的自动维护(protected $timestamps)       public $timestamps = false;       /*        * 分数表(ScoreInfo)与课程表(SobjectInfo)、学生信息表(StuInfo)有主外键关系        * 并且是一对多的关系        */        public function StuInfo(){            return $this -> belongsTo('StuInfo','s_id');        }        /*         * 定义逆向关系指向主键表         * */        public function SobjectInfo(){            return $this -&gt; belongsTo('SobjectInfo','soc_id');        }} ?&gt;
Copy after login
通过以上步骤的处理,表与表之间的一对多关系已确立,
下面将介绍在Laravel Administrato 后台中的实现 下拉列表查询、绑定等应用
<?phpreturn array(    'title' => '分数信息',        //栏目名    'single' =&gt; ' &gt;&gt;',            //新建描述    'model' =&gt; 'ScoreInfo',       //分数信息    'form_width' =&gt; 960,          //左边栏目宽    //列表    'columns' =&gt; array(        'so_id' =&gt; array(            'title' =&gt; '编号',            'select' =&gt; "so_id",            'sort_field'=&gt;'so_id'        ),        's_name'=&gt;array(            'title'=&gt;'学生姓名',            'relationship' =&gt; 'StuInfo',            'select' =&gt; '(:table).s_name',        ),       'soc_name'=&gt;array(            'title'=&gt;'课程名称',            'relationship' =&gt; 'SobjectInfo',            'select' =&gt; '(:table).soc_name',        ),        'score'=&gt;array(            'title'=&gt;'考试分数',            'select'=&gt;'score'        ),    ),    //筛选信息    'filters' =&gt; array(        'so_id' =&gt; array(            'title'=&gt;'编号'        ),        'SobjectInfo'=&gt;array(            'type'    =&gt; 'relationship',            'title'   =&gt; '<span style="font-family: Arial, Helvetica, sans-serif;">课程名</span><span style="font-family: Arial, Helvetica, sans-serif;">',</span>            'name_field' =&gt; 'soc_name',        ),        'StuInfo'=&gt;array(            'type'  =&gt; 'relationship',            'title' =&gt; '学生姓名',            'name_field'  =&gt; 's_name',        ),        'score'=&gt;array(            'title'=&gt;'考试分数',            'type' =&gt; 'number'        ),    ),    //修改、新增    'edit_fields' =&gt; array(        'StuInfo'=&gt;array(            'type'  =&gt; 'relationship',            'title' =&gt; '学生姓名',            'name_field'  =&gt; 's_name',        ),        'SobjectInfo'=&gt;array(            'type'    =&gt; 'relationship',            'title'   =&gt; '课程名',            'name_field' =&gt; 'soc_name',        ),        'score'=&gt;array(            'title'=&gt;'考试分数',            'type'=&gt;'text'        ),    ));?&gt;
Copy after login
以上示例展示的是 后台 分数信息 类。

示例中多次使用到 “学生姓名”、“课程名”,虽然他们存储在不同的表中,但由于我们之前在 Model中已建立了它们之间的 一对多关系,因此我们可以自由搭配组合

效果图如下:




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles