新聞中心
這篇文章主要介紹了laravel中表與表之間的關(guān)系是什么,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,成都做網(wǎng)站公司-成都創(chuàng)新互聯(lián)公司已向近千家企業(yè)提供了,網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)絡(luò)營銷等服務(wù)!設(shè)計(jì)與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗(yàn),合理的價(jià)格為您打造企業(yè)品質(zhì)網(wǎng)站。
首先關(guān)于表與表之間的關(guān)系
1.一對(duì)一
2.一對(duì)多
3.多對(duì)一
4.多對(duì)多
區(qū)分父表與子表
1.”一”的是父表
2.”多”的一方是子表
如何處理一對(duì)多關(guān)系
在子表中建一個(gè)字段(外鍵)指向父表
如何處理多對(duì)多關(guān)系
建立一張中間表,將”多對(duì)多”關(guān)系轉(zhuǎn)化為”一對(duì)多”
案例分析
表一: 用戶表(it_user)
表二: 用戶詳情表(it_user_info)
表三: 文章表(it_article)
表四: 國家表(it_country)
表五: 用戶角色表(it_role)
① 一對(duì)一
用戶表(表一)與詳情表(表二)就是一對(duì)一的關(guān)系
②一對(duì)多
用戶表(表一)與文章表(表三)就是一對(duì)多的關(guān)系
③多對(duì)一
用戶表(表一)與國家表(表四)就是多對(duì)一的關(guān)系
④多對(duì)多
用戶表(表一)與角色表(表五)就是多對(duì)多的關(guān)系
用戶表建表及測試數(shù)據(jù)
DROP TABLE IF EXISTS `it_user`;
CREATE TABLE `it_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`name` varchar(64) DEFAULT NULL COMMENT '用戶名',
`password` char(32) DEFAULT NULL COMMENT '密碼(不使用md5)',
`country_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `國家id` (`country_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of it_user
-- ----------------------------
INSERT INTO `it_user` VALUES ('1', 'xiaoming', '123456', '1');
INSERT INTO `it_user` VALUES ('2', 'xiaomei', '123456', '1');
INSERT INTO `it_user` VALUES ('3', 'xiaoli-new', '123', '1');用戶詳情表建表及測試數(shù)據(jù)
-- ----------------------------
-- Table structure for it_user_info
-- ----------------------------
DROP TABLE IF EXISTS `it_user_info`;
CREATE TABLE `it_user_info` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`tel` char(11) DEFAULT NULL,
`email` varchar(128) DEFAULT NULL,
`addr` varchar(255) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of it_user_info
-- ----------------------------
INSERT INTO `it_user_info` VALUES ('1', '13012345678', 'xiaoming@163.com', '北京');
INSERT INTO `it_user_info` VALUES ('2', '15923456789', 'xiaomei@163.com', '上海');
INSERT INTO `it_user_info` VALUES ('3', '18973245670', 'xiaoli@163.com', '武漢');文章表建表及測試數(shù)據(jù)
-- ----------------------------
-- Table structure for it_article
-- ----------------------------
DROP TABLE IF EXISTS `it_article`;
CREATE TABLE `it_article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`content` text,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of it_article
-- ----------------------------
INSERT INTO `it_article` VALUES ('1', '世界那么大,我想去看看', '錢包那么小,總是不夠', '1');
INSERT INTO `it_article` VALUES ('2', '我想撞角遇到愛', '卻是碰到鬼', '2');
INSERT INTO `it_article` VALUES ('3', '哈哈哈哈', '嘻嘻嘻嘻', '1');國家表建表及測試數(shù)據(jù)
-- ----------------------------
-- Table structure for it_country
-- ----------------------------
DROP TABLE IF EXISTS `it_country`;
CREATE TABLE `it_country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of it_country
-- ----------------------------
INSERT INTO `it_country` VALUES ('1', '中國');
INSERT INTO `it_country` VALUES ('2', '美國');用戶角色表建表及測試數(shù)據(jù)
-- ----------------------------
-- Table structure for it_role
-- ----------------------------
DROP TABLE IF EXISTS `it_role`;
CREATE TABLE `it_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of it_role
-- ----------------------------
INSERT INTO `it_role` VALUES ('1', '開發(fā)');
INSERT INTO `it_role` VALUES ('2', '測試');
INSERT INTO `it_role` VALUES ('3', '管理');用戶和角色中間表表建表及測試數(shù)據(jù)
-- ----------------------------
-- Table structure for it_user_role
-- ----------------------------
DROP TABLE IF EXISTS `it_user_role`;
CREATE TABLE `it_user_role` (
`user_id` int(11) DEFAULT NULL,
`role_id` int(11) DEFAULT NULL,
KEY `role_id` (`role_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of it_user_role
-- ----------------------------
INSERT INTO `it_user_role` VALUES ('1', '1');
INSERT INTO `it_user_role` VALUES ('1', '2');
INSERT INTO `it_user_role` VALUES ('1', '3');
INSERT INTO `it_user_role` VALUES ('2', '1');
INSERT INTO `it_user_role` VALUES ('3', '2');準(zhǔn)備工作
1、規(guī)劃路由
在routes/web.php下寫如下路由:
//ORM的關(guān)聯(lián)關(guān)系
Route::get('/orm/relation/{mode}','ORM\UserController@relation');2、在App/Http/Controllers/ORM/UserController.php編寫relation方法
public function relation($mode)
{
switch ($mode){
case '1_1':
{
//一對(duì)一
}
break;
case '1_n':
{
//一對(duì)多
}
break;
case 'n_1':
{
//多對(duì)一
}
break;
case 'n_n':
{
//多對(duì)多
}
break;
default;
}
}3、安裝debug調(diào)試工具
3.1使用composer命令安裝
composer require barryvdh/laravel-debugbar
3.2、修改config/app.php文件,加載debugbar到laravel到項(xiàng)目中,在’providers’數(shù)組中加入如下配置:
Barryvdh\Debugbar\ServiceProvider::class,
除了安裝debugbar調(diào)試工具外,也可以使用查詢監(jiān)聽:編providers/AppServiceProvider.php的boot方法中加入如下代碼
\DB::listen(function ($query) {
var_dump($query->sql);
var_dump($query->bindings);
echo '
';
});也可以打印出執(zhí)行的sql語句。
一對(duì)一
1、創(chuàng)建Userinfo模型對(duì)象
進(jìn)入cmd命令行進(jìn)入laravel項(xiàng)目所在目錄執(zhí)行以下命令
php artisan make:model Userinfo
會(huì)在App目錄下生成Userinfo.php
2、編輯Userinfo模型文件
3、編寫UserModel, 加入一對(duì)一方法
hasOne('App\Userinfo','user_id'); }4、編寫UserController, 調(diào)用一對(duì)一方法
public function relation($mode) { switch ($mode){ case '1_1': { //一對(duì)一 $data = UserModel::find(1)->Userinfo()->get(); dd($data); } break; default; } }一對(duì)多
1、創(chuàng)建article模型對(duì)象
執(zhí)行命令
php artisan make:model Article在app下生成Article.php文件
2、編寫article模型文件
3、編寫UserModel, 加入一對(duì)多方法
public function Artice() { return $this->hasMany('App\Article','User_id'); }4、編寫UserController,調(diào)用一對(duì)多方法
public function relation($mode) { switch ($mode){ case '1_1': { //一對(duì)一 $data = UserModel::find(1)->Userinfo()->get(); dd($data); } break; case '1_n': { //一對(duì)多 $data = UserModel::find(1)->Artice()->get(); dd($data); } break; default; } } }多對(duì)一
1、創(chuàng)建country模型對(duì)象
執(zhí)行命令
php artisan make:model Country2、編寫country模型文件
3、編寫UserModel, 加入多對(duì)一方法
public function Country() { return $this->belongsTo('App\Country','country_id'); }4、編寫UserController, 調(diào)用方法
public function relation($mode) { switch ($mode){ case '1_1': { //一對(duì)一 $data = UserModel::find(1)->Userinfo()->get(); dd($data); } break; case '1_n': { //一對(duì)多 $data = UserModel::find(1)->Artice()->get(); dd($data); } break; case 'n_1': { //多對(duì)一 $data = UserModel::find(1)->Country()->get(); dd($data); } break; default; } } }多對(duì)多
1、創(chuàng)建role模型對(duì)象
執(zhí)行命令
php artisan make:model Role執(zhí)行命令
php artisan make:model User_role2、編寫Role模型
編寫User_role模型
因?yàn)楸碇袥]有主鍵字段,所以需要兩個(gè)字段即可
3、編寫UserModel, 加入多對(duì)多方法
public function Role(){ /* * 第一個(gè)參數(shù):要關(guān)聯(lián)的表對(duì)應(yīng)的類 * 第二個(gè)參數(shù):中間表的表名 * 第三個(gè)參數(shù):當(dāng)前表跟中間表對(duì)應(yīng)的外鍵 * 第四個(gè)參數(shù):要關(guān)聯(lián)的表跟中間表對(duì)應(yīng)的外鍵 * */ return $this->belongsToMany('App\Role','user_role','user_id','role_id'); }4、編寫UserController, 調(diào)用多對(duì)多方法
public function relation($mode) { switch ($mode){ case '1_1': { //一對(duì)一 $data = UserModel::find(1)->Userinfo()->get(); dd($data); } break; case '1_n': { //一對(duì)多 $data = UserModel::find(1)->Artice()->get(); dd($data); } break; case 'n_1': { //多對(duì)一 $data = UserModel::find(1)->Country()->get(); dd($data); } break; case 'n_n': { //多對(duì)多 $data = UserModel::find(2)->Role()->get(); dd($data); } break; default; } }總結(jié):
1、一對(duì)一使用方法:hasOne()
2、一對(duì)多使用方法:hasMany()
3、多對(duì)一使用方法:belongsTo()
4、多對(duì)多使用方法:belongsToMany()
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“l(fā)aravel中表與表之間的關(guān)系是什么”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
當(dāng)前文章:laravel中表與表之間的關(guān)系是什么
本文鏈接:http://www.dlmjj.cn/article/ippojc.html


咨詢
建站咨詢
