【问题标题】:How do you make a Save button work in Laravel (DataTables)?你如何使保存按钮在 Laravel(DataTables)中工作?
【发布时间】:2021-08-14 18:18:50
【问题描述】:

如何在我的控制器中创建一个 php 函数来保存由复选框指示的特定行?单击提交按钮时,需要将此行数据附加到 SQL 数据库。这是我迄今为止尝试过的,但它似乎不起作用

@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">
<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">
<input type="checkbox" class="form-check-input" id="tableMaterialCheck2">
</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">
<tr>
<td><button type="submit" id="btn-save" value="create" class="btn  btn-sm">opslaan</button></td>
</tr>
</div>
</div>
</div>
</div>
</div>
@endsection

这是控制器,我不知道要在我的函数中添加什么!

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\Contracts\StageVoorstelRepository;
class student_bekijkstagesController extends Controller
{
    /** 
     * @var StagevoorstelRepository
     */
    private $stageVoorstel;
    /**
     * kaartjesWeergevenTestController Constructor
     *  
     * @param StageVoorstelRepository $stagevoorstel
     */
    public function __construct(StageVoorstelRepository $stageVoorstel)
    {
        //$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"){
            
        }
    }
}

【问题讨论】:

  • 我没有看到表单 html 标记。是否包含在视图中??
  • 我没用过!

标签: php html laravel visual-studio-code datatables


【解决方案1】:

我没有看到任何表单标签。

<form action="{{route ('route_name')}}" method="route_method">
...
</form>

您还需要@csrf 用于 post、put、... 方法。

【讨论】:

  • 这是一个实际的答案吗?或更多评论?请使用评论澄清案例,然后提出实际有用的答案。
  • @Ron 这是一个答案。哪一个更容易?只需在js中添加表单标签或编写额外的代码行???
【解决方案2】:

由于你没有使用表单,你可以利用JS来收集checked row和数据,并通过JS方法或Jquery的.post()POST收集到你的后端PHP,这样数据就可以保存了在数据库中

有这样的例子:https://datatables.net/forums/discussion/42290/getting-the-checked-rows-in-datatables 它描述了如何从数据表中获取选中的行并使用它。 Datatables 文档也有大量示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多