SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('1', 'admin', 'admin@163.com', '$2y$10$J/yXqscucanrHAGZp9G6..Tu1Md.SOljX3M8WrHsUdrgat4zeSuhC', 'ilocXtjZJwhrmIdLG1cKOYegeCwQCkuyx1pYAOLuzY2PpScQFT5Ss7lBCi7i', '2016-04-21 16:26:23', '2016-12-14 09:29:59');
INSERT INTO `users` VALUES ('2', 'baidu', '10940370@qq.com', '$2y$10$2A5zJ4pnJ5uCp1DN3NX.5uj/Ap7P6O4nP2BaA55aFra8/rti1K6I2', null, '2016-04-22 06:48:10', '2016-04-22 06:48:10');
INSERT INTO `users` VALUES ('3', 'fantasy', '1009@qq.com', '', null, '2017-06-14 10:38:57', '2017-06-15 10:39:01');
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for roles
-- ----------------------------
DROP TABLE IF EXISTS `roles`;
CREATE TABLE `roles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Records of roles
-- ----------------------------
INSERT INTO `roles` VALUES ('1', '超级版主', '2016-04-21 16:26:23', '2016-12-14 09:29:59');
INSERT INTO `roles` VALUES ('2', '司令', '2016-04-22 06:48:10', '2016-04-22 06:48:10');
INSERT INTO `roles` VALUES ('3', '军长', '2017-06-14 10:38:57', '2017-06-15 10:39:01');
INSERT INTO `roles` VALUES ('4', '司长', '2017-06-07 10:41:41', '2017-06-15 10:41:51');
INSERT INTO `roles` VALUES ('5', '团战', '2017-06-22 10:41:44', '2017-06-28 10:41:54');
INSERT INTO `roles` VALUES ('6', '小兵', '2017-06-22 10:41:47', '2017-06-22 10:41:56');
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for role_user
-- ----------------------------
DROP TABLE IF EXISTS `role_user`;
CREATE TABLE `role_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`role_id` int(11) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of role_user
-- ----------------------------
INSERT INTO `role_user` VALUES ('1', '1', '2', '2017-06-07 11:42:13', '2017-06-21 11:32:16');
INSERT INTO `role_user` VALUES ('2', '1', '3', '2017-06-07 11:32:13', '2017-06-07 11:22:13');
INSERT INTO `role_user` VALUES ('3', '2', '4', '2017-06-07 11:32:13', '2017-06-07 11:12:13');
INSERT INTO `role_user` VALUES ('4', '1', '5', '2017-06-07 11:32:13', '2017-06-07 11:22:13');
INSERT INTO `role_user` VALUES ('5', '3', '6', '2017-06-07 11:32:13', '2017-06-07 11:52:13');
INSERT INTO `role_user` VALUES ('6', '3', '2', '2017-06-07 11:32:13', '2017-06-07 11:42:13');
INSERT INTO `role_user` VALUES ('7', '2', '2', '2017-06-07 11:42:13', '2017-06-07 11:52:13');
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class Role
* @package App\Models
* @mixin \Eloquent
*/
class Role extends Model
{
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class User
* @package App\Models
* @mixin \Eloquent
*/
class User extends Model
{
/**
* 用户角色
*/
public function roles()
{
return $this->belongsToMany('App\Models\Role');
}
}
<?php $user = User::find(1); $roles = $user->roles; echo '用户'.$user->name.'所拥有的角色:'; foreach($roles as $role) echo $role->name.' '; //对应输出为:用户admin所拥有的角色:司令 军长 团战
User::find(1)->roles()->orderBy('name')->get();
return $this->belongsToMany('App\Models\Role', 'user_roles');
return $this->belongsToMany('App\Models\Role', 'user_roles', 'user_id', 'role_id');
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class Role
* @package App\Models
* @mixin \Eloquent
*/
class Role extends Model
{
/**
* 角色用户
*/
public function users()
{
return $this->belongsToMany('App\Models\User');
}
}
$role = Role::find(2); $users = $role->users; echo '角色#'.$role->name.'下面的用户:'; foreach ($users as $user) echo $user->name.' ';//对应输出为:角色#司令下面的用户:admin fantasy baidu
$roles = User::find(1)->roles; foreach ($roles as $role) echo $role->pivot->role_id.'<br>';//对应输出为:2 3 5
return $this->belongsToMany('App\Models\Role')->withPivot('column1', 'column2');
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class User
* @package App\Models
* @mixin \Eloquent
*/
class User extends Model
{
/**
* 用户角色
*/
public function roles()
{
//return $this->belongsToMany('App\Models\Role');
return $this->belongsToMany('App\Models\Role')->withPivot('username');
}
}
$user = User::find(1); foreach ($user->roles as $role) echo $role->pivot->username;//对应输出为:马特马特2马特3
return $this->belongsToMany('App\Models\Role')->withTimestamps();
return $this->belongsToMany('App\Models\Role')->withPivot('username')->wherePivot('username', '马特2');
//return $this->belongsToMany('App\Models\Role')->wherePivotIn('role_id', [1, 2]);
$user = User::find(1); print_r($user->roles->toArray());
Array
(
[0] => Array
(
[id] => 3
[name] => 军长
[created_at] => 2017-06-14 10:38:57
[updated_at] => 2017-06-15 10:39:01
[pivot] => Array
(
[user_id] => 1
[role_id] => 3
[username] => 马特2
)
)
)
return $this->belongsToMany('App\Models\Role')->withTimestamps();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有