Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理
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'); }}?>
定义与之对应的逆向关系 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 -> belongsTo('SobjectInfo','soc_id'); }} ?>
下面将介绍在Laravel Administrato 后台中的实现 下拉列表查询、绑定等应用
<?phpreturn array( 'title' => '分数信息', //栏目名 'single' => ' >>', //新建描述 'model' => 'ScoreInfo', //分数信息 'form_width' => 960, //左边栏目宽 //列表 'columns' => array( 'so_id' => array( 'title' => '编号', 'select' => "so_id", 'sort_field'=>'so_id' ), 's_name'=>array( 'title'=>'学生姓名', 'relationship' => 'StuInfo', 'select' => '(:table).s_name', ), 'soc_name'=>array( 'title'=>'课程名称', 'relationship' => 'SobjectInfo', 'select' => '(:table).soc_name', ), 'score'=>array( 'title'=>'考试分数', 'select'=>'score' ), ), //筛选信息 'filters' => array( 'so_id' => array( 'title'=>'编号' ), 'SobjectInfo'=>array( 'type' => 'relationship', 'title' => '<span style="font-family: Arial, Helvetica, sans-serif;">课程名</span><span style="font-family: Arial, Helvetica, sans-serif;">',</span> 'name_field' => 'soc_name', ), 'StuInfo'=>array( 'type' => 'relationship', 'title' => '学生姓名', 'name_field' => 's_name', ), 'score'=>array( 'title'=>'考试分数', 'type' => 'number' ), ), //修改、新增 'edit_fields' => array( 'StuInfo'=>array( 'type' => 'relationship', 'title' => '学生姓名', 'name_field' => 's_name', ), 'SobjectInfo'=>array( 'type' => 'relationship', 'title' => '课程名', 'name_field' => 'soc_name', ), 'score'=>array( 'title'=>'考试分数', 'type'=>'text' ), ));?>
示例中多次使用到 “学生姓名”、“课程名”,虽然他们存储在不同的表中,但由于我们之前在 Model中已建立了它们之间的 一对多关系,因此我们可以自由搭配组合
效果图如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
