這是轉載的文章
出處:https://blog.csdn.net/weixin_38112233/article/details/79220535
作者:重新遇到
一。建表和插入測試資料
1.使用者表建表及測試資料
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;
插入測試資料
INSERT INTO `it_user` VALUES ('', 'xiaoming', '', '');
INSERT INTO `it_user` VALUES ('', 'xiaomei', '', '');
INSERT INTO `it_user` VALUES ('', 'xiaoli-new', '', '');
2.使用者詳情表建表及測試資料
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;
插入測試資料
INSERT INTO `it_user_info` VALUES ('', '', '[email protected]', '北京');
INSERT INTO `it_user_info` VALUES ('', '', '[email protected]', '上海');
INSERT INTO `it_user_info` VALUES ('', '', '[email protected]', '武漢');
3文章表建表及測試資料
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;
插入測試資料
INSERT INTO `it_article` VALUES ('', '世界那麼大,我想去看看', '錢包那麼小,總是不夠', '');
INSERT INTO `it_article` VALUES ('', '我想撞角遇到愛', '卻是碰到鬼', '');
INSERT INTO `it_article` VALUES ('', '哈哈哈哈', '嘻嘻嘻嘻', '');
4.國家表建表及測試資料
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;
插入測試資料
INSERT INTO `it_country` VALUES ('', '中國');
INSERT INTO `it_country` VALUES ('', '美國');
5.使用者角色表建表及測試資料
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;
插入測試資料
INSERT INTO `it_role` VALUES ('', '開發');
INSERT INTO `it_role` VALUES ('', '測試');
INSERT INTO `it_role` VALUES ('', '管理');
6.使用者和角色中間表表建表及測試資料
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;
插入測試資料
INSERT INTO `it_user_role` VALUES ('', '');
INSERT INTO `it_user_role` VALUES ('', '');
INSERT INTO `it_user_role` VALUES ('', '');
INSERT INTO `it_user_role` VALUES ('', '');
INSERT INTO `it_user_role` VALUES ('', '');
二設定表字首
開啟config/database.php
'prefix' => env('DB_PREFIX', ''),
然後在.env檔案中加入DB_PREFIX=it_
執行命令
php artisan make:controller ORM\UserController
<?php namespace App\Http\Controllers\ORM; use Illuminate\Http\Request;
use App\Http\Controllers\Controller; class UserController extends Controller
{
public function relation($mode)
{
switch ($mode){
case '1_1':
{
//一對一 }
break;
case '1_n':
{
//一對多 }
break;
case 'n_1':
{
//多對一 }
break;
case 'n_n':
{
//多對多 }
break;
default;
} } }
php artisan make:model UserModel
Model created successfully.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class UserModel extends Model
{
protected $table = 'user';//真是表名
protected $primaryKey = 'id';//主鍵欄位,預設為id
protected $fillable = ['name','password'];//可以操作的欄位
public $timestamps = false;//如果資料表中沒有created_at和updated_id欄位,則$timestamps則可以不設定,預設為true public function Userinfo()
{
/*
* @param [string] [name] [需要關聯的模型類名]
* @param [string] [foreign] [引數一指定資料表中的欄位]
* */
return $this->hasOne('App\Userinfo','user_id');
} }
php artisan make:model Userinfo
Model created successfully.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Userinfo extends Model
{
protected $table = 'user_info';
protected $primaryKey = 'user_id';
protected $fillable = ['user_id','tel','email','addr'];
public $timestamps = false;
}
新增路由
Route::get('/orm/relation/{mode}','ORM\UserController@relation');
三。編寫UserController, 呼叫一對一方法
<?php namespace App\Http\Controllers\ORM; use App\UserModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller; class UserController extends Controller
{
public function relation($mode)
{
switch ($mode){
case '1_1':
{
//一對一
$data = UserModel::find(1)->Userinfo()->get();
dd($data);
}
break;
case '1_n':
{
//一對多 }
break;
case 'n_1':
{
//多對一 }
break;
case 'n_n':
{
//多對多 }
break;
default;
} } }
http://127.0.0.1/fun/public/orm/relation/1_1
php artisan make:model Article
Model created successfully.
編寫Article模型
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model
{
protected $table = 'article';
protected $primaryKey = 'id';
protected $fillable = ['id','title','content','user_id'];
public $timestamps = false; }
編寫UserModel, 加入一對多方法
public function Artice()
{
return $this->hasMany('App\Article','User_id');
}
編寫UserController,呼叫一對多方法
case '1_n':
{
//一對多
$data = UserModel::find(1)->Artice()->get();
dd($data); }
break;
http://127.0.0.1/fun/public/orm/relation/1_n
php artisan make:model Country
Model created successfully.
、編寫country模型檔案
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Country extends Model
{ protected $table = 'country'; //真實表名
protected $primaryKey = "id"; //主鍵id
protected $fillable = ['id','name']; //允許操作的欄位
public $timestamps =false; //如果資料表中沒有created_at和updated_id欄位,則$timestamps則可以不設定,預設為true }
編寫UserModel, 加入多對一方法
public function Country()
{
return $this->belongsTo('App\Country','country_id');
}
編寫UserController, 呼叫方法
case 'n_1':
{
//多對一
//多對一
$data = UserModel::find(1)->Country()->get();
dd($data); }
break;
http://127.0.0.1/fun/public/orm/relation/n_1
1、建立role模型物件
執行命令
php artisan make:model Role
執行命令
php artisan make:model User_role
2、編寫Role模型
namespace App; use Illuminate\Database\Eloquent\Model; class Role extends Model
{
protected $table = 'role';
protected $primaryKey = "id";
protected $fillable = ['name'];
public $timestamps =false;
}
編寫User_role模型
因為表中沒有主鍵欄位,所以需要兩個欄位即可
<?php namespace App; use Illuminate\Database\Eloquent\Model; class User_role extends Model
{
protected $table = 'user_role';
public $timestamps =false; }
3、編寫UserModel, 加入多對多方法
public function Role(){
/*
* 第一個引數:要關聯的表對應的類
* 第二個引數:中間表的表名
* 第三個引數:當前表跟中間表對應的外來鍵
* 第四個引數:要關聯的表跟中間表對應的外來鍵
* */
return $this->belongsToMany('App\Role','user_role','user_id','role_id');
}
4、編寫UserController, 呼叫多對多方法
case 'n_n':
{
//多對多
$data = UserModel::find(2)->Role()->get();
dd($data); }
break;
http://127.0.0.1/fun/public/orm/relation/n_n