Laravel DataTables如何实现Save按钮功能 保存勾选行到SQL数据库
实现步骤
1. 修复前端Blade模板
你当前的模板缺少表单提交容器、复选框未绑定行ID,调整后代码如下:
@extends('layouts.app') @include('partialViews.navigation') @section('content') <div class="container"> <div class="row justify-content-center"> @yield('navigation') <div class="col-md-8"> <div class="card"> <!-- 新增表单标签,指定提交路由 --> <form action="{{ route('stage.save') }}" method="POST"> @csrf <table id="example" class="data-table table table-striped"> <thead> <tr> <th scope="col"></th> <th scope="col">Stage</th> <th scope="col">Bedrijf</th> <th scope="col">Afdeling</th> <th scope="col">Locatie</th> <th scope="col">Periode</th> </tr> </thead> <tbody> @foreach ($stagevoorstelen as $key => $stagevoorstel) <tr> <td scope="row"> <div class="form-check"> <!-- 给复选框加name数组、绑定当前行ID、唯一ID --> <input type="checkbox" class="form-check-input" name="selected_ids[]" value="{{ $stagevoorstel->id }}" id="check_{{ $stagevoorstel->id }}"> </div> </td> <td>{{$stagevoorstel->assignment->title ?? "stagair"}}</td> <td>{{$stagevoorstel->companyContact1->department->company->name}}</td> <td>{{$stagevoorstel->companyContact1->department->name ?? ""}}</td> <td>{{$stagevoorstel->companyContact1->department->company->city}}</td> <td>{{$stagevoorstel->assignment->year ?? ""}} {{$stagevoorstel->assignment->semester ?? ""}}</td> </tr> @endforeach </tbody> </table> <div id="buttontjes" class="p-3"> <!-- 调整提交按钮value和控制器判断逻辑匹配 --> <button type="submit" name="submit" value="Save" id="btn-save" class="btn btn-sm">opslaan</button> </div> </form> </div> </div> </div> </div> @endsection
2. 新增路由
在routes/web.php中添加POST提交路由:
Route::post('/stage/save', [\App\Http\Controllers\student_bekijkstagesController::class, 'save'])->name('stage.save');
3. 完善控制器save方法
首先确认你要存储选中数据的目标表对应的模型(示例中假设模型为App\Models\StudentSelectedStage,对应存储学生选中的实习记录),调整后代码如下:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\StudentSelectedStage; use App\Repositories\Contracts\StageVoorstelRepository; class student_bekijkstagesController extends Controller { /** * @var StagevoorstelRepository */ private $stageVoorstel; /** * @param StageVoorstelRepository $stageVoorstel */ public function __construct(StageVoorstelRepository $stageVoorstel) { // 启用auth中间件获取当前登录学生ID $this->middleware('auth'); $this->stageVoorstel = $stageVoorstel; } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { return view('student_bekijkstages',['stagevoorstelen' => $this->stageVoorstel->getAvailableForStudents()]); } public function save(Request $request){ if($request->submit == "Save"){ // 校验提交参数 $validated = $request->validate([ 'selected_ids' => 'required|array', 'selected_ids.*' => 'exists:stagevoorstels,id' // 这里的stagevoorstels替换为你实习表的真实表名 ]); // 组装待插入数据,关联当前登录学生ID $insertData = collect($validated['selected_ids'])->map(function($stageId) { return [ 'student_id' => auth()->id(), 'stagevoorstel_id' => $stageId, 'created_at' => now(), 'updated_at' => now() ]; })->toArray(); // 批量插入到数据库 StudentSelectedStage::insert($insertData); // 返回成功提示 return back()->with('success', '选中的实习记录已保存'); } return back()->withErrors('提交参数错误'); } }
注意事项
- 确保目标存储模型的
$fillable属性已经配置了允许批量赋值的字段 - 如果需要避免同一个学生重复保存同一条实习记录,可以在目标表加
student_id和stagevoorstel_id的联合唯一索引 - 示例中的表名、模型名请替换为你项目内的实际命名
内容的提问来源于stack exchange,提问作者Conf007
相关产品推荐
相关产品推荐

