Toggle navigation
菜菜小站
菜菜小站
前端开发
前端开发
前端基础
bootstrap
JavaScript
微信小程序
后端开发
后端开发
php开发
laravel
数据库
编辑器
git
微信开发
服务器
服务器
https
cos云存储
ubuntu
homestead
go
ContactMe
laravel 无限极分类
作者:
菜菜子
|
时间:2018-09-01 21:17:00 |
分类:
后端开发
,
laravel
|
访问: 1,692 次|
评论:
0 评论
需要实现一个无限极分销的功能,此外这个分销还存在一个断链的情况 举例: 下面的parent_user_id表示上级用户id,grade表示当前用户等级,共四级(0普通会员 1铜牌会员 2银牌会员 3金牌会员) * 情况1: | id | parent_user_id | grade | | -- | ---- | --- | | 1 | | 0 | | 2 | 1 | 0 | | 3 | 2 | 0 | | 4 | 3 | 0 | 那么现在id为1的这个用户的所有下线就是id为2,3,4的用户(即如果查询用户为普通用户,那么所有下线用户应该都为普通会员,此时下线会员等级与自己相同或者更低) * 情况2: | id | parent_user_id | grade | | -- | ---- | --- | | 1 | | 0 | | 2 | 1 | 0 | | 3 | 2 | 1 | | 4 | 3 | 0 | 那么现在id为1的这个用户的所有下线就是id为2的用户(即如果查询用户**为普通用户**,那么所有下线用户应该都为普通会员,此时下线会员等级与自己相同或者更低) * 情况3: | id | parent_user_id | grade | | -- | ---- | --- | | 1 | | 1 | | 2 | 1 | 0 | | 3 | 2 | 1 | | 4 | 3 | 0 | 那么现在id为1的这个用户的所有下线就是id为2的用户(即如果查询用户**不为普通用户**,那么所有下线用户应该都为比自己级别低的用户,不能与查询用户级别相等) * 情况4: | id | parent_user_id | grade | | -- | ---- | --- | | 1 | | 2 | | 2 | 1 | 0 | | 3 | 2 | 1 | | 4 | 3 | 0 | 那么现在id为1的这个用户的所有下线就是id为2,3,4的用户(即如果查询用户**不为普通用户**,那么所有下线用户应该都为比自己级别低的用户,不能与查询用户级别相等) 简单来说就是如果下线用户升级以后,这条链路就断开了。 ### 实现方法 #### 第一步建表: ```php increments('id'); $table->string('nickname')->comment('昵称'); $table->string('phone')->unique()->comment('手机号'); $table->string('password'); $table->string('parent_user_id')->nullable()->comment('推荐人id'); $table->enum('grade', [0,1,2,3]])->default(0)->comment('会员等级'); $table->string('recommend_no')->unique()->comment('推荐码'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } ``` ### 模型 ```php '普通会员', self::GRADE_TWO => '铜牌会员', self::GRADE_THREE => '银牌会员', self::GRADE_FOUR => '金牌会员', ]; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'nickname', 'phone', 'password', 'recommend_no', 'parent_user_id' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; public function getJWTIdentifier() { return $this->getKey(); } public function getJWTCustomClaims() { return []; } public function getQrcodeAddressAttribute($value) { return getAttributes($value); } /**上线start*************************************************************************/ //上级推荐人(不论等级) public function parent_user() { return $this->belongsTo(get_class($this)); } //上级推荐人(论等级) public function parentUserUp() { $parent_user = $this->parent_user;//上级用户 // dd($parent_user->grade); if (!blank($parent_user)) { if ($this->grade == 0) { //如果被查询用户的是普通用户,那就直接返回父级用户 return $parent_user; } else { //如果查询的用户不是普通用户,那就只查比自己等级高的父级 if ($parent_user->grade > $this->grade) { //父级用户比自己级别大才返回该父级用户 return $parent_user; } else { //否则返回null return null; } } } else { return null; } } //所有上级推荐人(论等级) //如果用户是普通会员就不断的上查,如该用户是普通会员,上级用户也是普通会员,上上级用户也是普通会员,上上上级用户也是普通会员,那么这些上级用户都能拿到返现 //如果用户不是普通会员,如该用户是铜牌会员,那就查上级用户是银牌,如果上级用户是银牌,那就查上上级用户是银牌的,依次类推... public function allParentUserUp() { $result = new Collection; $parent = $this->parentUserUp(); // dd($parent); if (!blank($parent)) { $result->push($parent); dump($parent); $all_parent_users = $parent->allParentUserUp(); if (!blank($all_parent_users)) { foreach ($all_parent_users as $subChild) { $result->push($subChild); } } } return $result; } /**end*************************************************************************/ /**start*************************************************************************/ //所有比自己等级低的通过自己推荐码进来的用户(当自己为普通会员时除外) public function children_down_users() { $all_child = $this->hasMany(get_class($this), 'parent_user_id'); if ($this->grade == 0) { return $all_child->where('grade', '=', $this->grade); } else { return $all_child->where('grade', '<', $this->grade); } } //该用户的所有下线用户集合 public function all_children_down_users() { $result = new Collection; $children = $this->children_down_users; foreach ($children as $child) { $result->push($child); $childResult = $child->all_children_down_users(); foreach ($childResult as $subChild) { $result->push($subChild); } } return $result; } /**end*************************************************************************/ } ``` ### 使用方法 查找比这个用户低的所有下线子用户 ```php auth()->user()->all_children_down_users() ``` 查找这个用户的所有上线用户 ```php auth()->user()->allParentUserUp() ``` ### helpful 集合分页方法: https://www.cc430.cn/index.php/archives/635/ 参考: https://laravel-china.org/articles/12466/unlimited-classification
标签:
无限极分类
,
分销
还不快抢沙发
添加新评论
昵称(必填)
邮箱(必填)
网站(选填)
内容(必填)
提交评论
最新文章
部署go项目
lumen多个项目共用代码
lumen >=5.8中使用pusher广播
nginx负载均衡配置(二)
nginx负载均衡配置(一)
laravel中redis发布订阅相关问题处理
mysql 主从备份(一)
最新回复
DeWjjj: 兄弟很好使,奥力给!
懒猫爱伸腰: 大佬,问题我解决了,就加了一行代码 把ap-nanj...
懒猫爱伸腰: 大佬,啥会儿更新一下,现在又南京的桶了。。。。。
菜菜子: https://laravel-admin.org/d...
庆: laravel-admin 中图片上传如何限制图片尺寸...
惹我: 123
zhwangart: 感谢作者分享,这个问题简直烦死个人!终于解决~
世界和平: 大佬,可以加一个压缩图片后再上传到OSS的功能吗?我看...
拾一: 大部分文件上传都是转圈圈。。一直提示不成功
时年: 好了好了,多谢
标签
bootstrap
前端框架
laravel5
laravel
laravel-admin
gd库
微信小程序
表单
https
git
mysql
wamp配置
ajax
cos小工具
cos
cos插件
微信支付
swoole
redis
邮件发送
腾讯云
sql语言
jwt
typecho
数组
破解
编辑器
ubuntu
elasticsearch
vue
homestead
linux
websocket
nginx
docker
负载均衡
标签
刷新
加密处理
验证码
悬浮框
权限控制
markdown
shell
ui
任务调度
定时任务
deployer
gogs
反向代理
go
ftp
测试
分页
队列
微信公众号
日志
wnmp
vagrant
无限极分类
分销
集合
supervisor
部署
grid
python
chunk
api管理工具
oss
缓存
pusher
laravel-push
归档
2020年11月
2020年07月
2020年05月
2020年01月
2019年12月
2019年11月
2019年10月
2019年09月
2019年08月
2019年07月
2019年06月
2019年05月
2019年03月
2019年01月
2018年12月
2018年11月
2018年10月
2018年09月
2018年08月
2018年07月
2018年06月
2018年05月
2018年04月
2018年03月
2017年09月
2017年06月
2017年05月
2017年04月
2017年03月
2017年02月
2017年01月
2016年12月
2016年11月
友情链接
空
//