Toggle navigation
菜菜小站
菜菜小站
前端开发
前端开发
前端基础
bootstrap
JavaScript
微信小程序
后端开发
后端开发
php开发
laravel
数据库
编辑器
git
微信开发
服务器
服务器
https
cos云存储
ubuntu
homestead
go
ContactMe
laravel-admin列表中取多对多中间表字段
作者:
菜菜子
|
时间:2018-08-04 12:46:09 |
分类:
前端开发
|
访问: 5,394 次|
评论:
0 评论
一个场景,学校可以开多门课程,一个学生可以选多门课,同时一门课可以被多个学生选择,这样就属于多对多的关系,然后这门课老师后期还要给学生成绩,记录到课情况,这样这些信息就得记录在中间表中。展示的时候得把这些中间表字段进行展示 ### 数据库设计 课程表 ```php increments('id'); $table->unsignedInteger('course_id'); $table->string('name'); $table->tinyInteger('question_num')->comment('题目数量'); $table->tinyInteger('active')->default(0)->comment('状态:1发布中,0未发布'); $table->date('start_at')->comment('上课起始时间')->nullable(); $table->date('end_at')->comment('上课结束时间')->nullable(); $table->tinyInteger('chapter_type')->default(0)->comment('项目类型 0正常项目 1补考项目'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('chapters'); } } ``` 学生表 ```php increments('id'); $table->string('student_number')->comment('学号')->unique(); $table->string('nickname')->comment('昵称')->nullable(); $table->enum('gender',['男','女'])->comment('性别')->nullable(); $table->unsignedInteger('department_id')->comment('学院id')->nullable(); $table->unsignedInteger('class_id')->comment('班级id')->nullable(); $table->timestamp('enter_time')->comment('入学时间')->nullable(); $table->tinyInteger('active')->comment('账号状态:1正常,0冻结')->default(1); $table->string('password'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('students'); } } ``` 中间表 ```php increments('id'); $table->unsignedInteger('chapter_id'); $table->unsignedInteger('student_id'); $table->unsignedInteger('batch_id')->nullable()->comment('批次id');; $table->unsignedInteger('reservation_date_id')->nullable()->comment('预约日期id'); $table->date('reservation_date')->nullable()->comment('预约日期'); $table->unsignedInteger('sit_num')->nullable()->comment('座位号'); $table->unsignedInteger('status')->default(0)->comment('状态 0未上(开)课 1正常 2迟到 '); $table->timestamp('attend_time')->comment('上课打卡的时间')->nullable(); $table->timestamp('over_time')->comment('下课打卡的时间')->nullable(); $table->unsignedInteger('operation_score')->default(0)->comment('操作分'); $table->unsignedInteger('report_score')->default(0)->comment('报告分'); $table->timestamps(); }); \DB::statement("ALTER TABLE `student_chapters` comment ''"); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('student_chapters'); } } ``` ### 模型 学生模型student.php ```php //该生预约的实验课及其日期批次信息 public function chapter() { return $this->belongsToMany(Chapter::class, 'student_chapters')->withPivot( 'batch_id', 'reservation_date_id', 'reservation_date', 'sit_num', 'status', 'attend_time', 'over_time' ); } ``` 课程模型Chapter.php ```php public function students() { return $this->belongsToMany(Student::class, 'student_chapters','chapter_id')->withPivot( 'batch_id', 'reservation_date_id', 'reservation_date', 'sit_num', 'status', 'attend_time', 'over_time'); } ``` ### laravel-admin控制器修改 课程控制器ChapterController.php 列表grid方法里面 ```php $grid->actions(function ($actions) use ($courseId) { $keyId = $actions->getKey(); $actions->prepend('
'); $actions->prepend('
实验课学生管理
'); $actions->disableDelete(); }); ``` 添加路由routes.php ``` $router->resource('course/chapter/{chapter_id}/student','Lab\Course\ChapterStudentController');//实验项目的学生 ``` 这门实验课的学生ChapterStudentController.php ```php header('实验课学生'); $content->description('列表'); $content->body($this->grid($chapterId)); }); } /** * Edit interface. * * @param $id * @return Content */ public function edit($id) { return Admin::content(function (Content $content) use ($id) { $content->header('实验课学生'); $content->description('编辑'); $content->body($this->form()->edit($id)); }); } /** * Make a grid builder. * * @return Grid */ protected function grid($chapterId) { #传入的模型改为Chapter::find($chapterId) return Admin::grid(Chapter::find($chapterId), function (Grid $grid) { #找到该课所有student $grid->model()->students(); $grid->id('ID')->sortable(); $grid->nickname('姓名'); #获取pivot $grid->pivot('座位号')->display(function ($pivot) { return $pivot['sit_num']; }); #获取预约日期 $grid->reservation_date('预约日期')->display(function () { return $this->pivot['reservation_date']; }); $grid->batch_id('批次')->display(function () { $batch_id = $this->pivot['batch_id']; }); $grid->attend_time('上课打卡时间')->display(function () { return $this->pivot['attend_time']; }); $grid->attend_time('下课打卡时间')->display(function () { return $this->pivot['over_time']; }); $grid->filter(function ($filter) { $filter->disableIdFilter(); }); }); } } ```
标签:
laravel
,
laravel-admin
还不快抢沙发
添加新评论
昵称(必填)
邮箱(必填)
网站(选填)
内容(必填)
提交评论
最新文章
部署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月
友情链接
空
//