编程学习网 > PHP技术 > laravel > laravel-admin列表中取多对多中间表字段
2021
07-16

laravel-admin列表中取多对多中间表字段


一个场景,学校可以开多门课程,一个学生可以选多门课,同时一门课可以被多个学生选择,这样就属于多对多的关系,然后这门课老师后期还要给学生成绩,记录到课情况,这样这些信息就得记录在中间表中。展示的时候得把这些中间表字段进行展示


数据库设计

课程表


<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

/**

* 课程实验项目表

* Class CreateChaptersTable

*/

class CreateChaptersTable extends Migration

{

   /**

    * Run the migrations.

    *

    * @return void

    */

   public function up()

   {

       Schema::create('chapters', function (Blueprint $table) {

           $table->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

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

use Carbon\Carbon;

/**

* 学生表

* Class CreateStudentsTable

*/

class CreateStudentsTable extends Migration

{

   /**

    * Run the migrations.

    *

    * @return void

    */

   public function up()

   {

       Schema::create('students', function (Blueprint $table) {

           $table->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

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

/**

* 选课-预考核-预约-上课-评教

* Class CreateStudentChaptersTable

*/

class CreateStudentChaptersTable extends Migration

{

   /**

    * Run the migrations.

    *

    * @return void

    */

   public function up()

   {

       Schema::create('student_chapters', function (Blueprint $table) {

           $table->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


  //该生预约的实验课及其日期批次信息

   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


   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方法里面


 $grid->actions(function ($actions) use ($courseId) {

               $keyId = $actions->getKey();

               $actions->prepend('<br>');

               $actions->prepend('<a href="/admin/course/chapter/' . $keyId . "/student" . '">实验课学生管理</a>');

               $actions->disableDelete();

           });


添加路由routes.php


$router->resource('course/chapter/{chapter_id}/student','Lab\Course\ChapterStudentController');//实验项目的学生


这门实验课的学生ChapterStudentController.php


<?php

namespace App\Admin\Controllers\Lab\Course;

use App\Http\Controllers\Controller;

use App\Models\Chapter;

use Encore\Admin\Controllers\ModelForm;

use Encore\Admin\Layout\Content;

use Encore\Admin\Facades\Admin;

use Encore\Admin\Grid;

use Encore\Admin\Form;

use function foo\func;

class ChapterStudentController extends Controller

{

   use ModelForm;

   public $chapterId;

   /**

    * Index interface.

    *

    * @return Content

    */

   public function index($chapterId)

   {

       return Admin::content(function (Content $content) use ($chapterId) {

           $content->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-admin列表中取多对多中间表字段”的详细内容,想要获取更多laravel教程欢迎关注编程学习网

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取