我目前正在尝试将数据从数据表中保存回数据库。我正在处理数据表中的表单字段,并希望更改输入值并保存回数据库。理想情况下,我想要的是能够保存到数据库中的另一个表,如果id不存在但是现在我只是尝试进行更改并将其恢复到原始表。 我正在使用laravel 5.4和datatables插件。 我一直在寻找关于laracasts,堆栈溢出和数据表的各种帖子。我将我的数据保存在我的控制器中并继续获得一个对象作为回报。我已经尝试在我的模型中更改受保护的$ casts以查看我在开发工具中获得的值,但它没有帮助。我也在我的控制器中尝试了多种保存方法但没有用。请参阅下面的回复和代码:
路线:
Route::get('/plunker', ['uses'=>'Controller@plunker']);
Route::get('/plunker/ttotgetposts', ['as'=>'plunker.ttotgetposts','uses'=>'Controller@ttotGetPosts']);
Route::post('/plunker', 'Controller@saveOrUpdate')->name('saveOrUpdate');
型号:
namespace App;
use Illuminate\Database\Eloquent\Model;
use \DB;
class Inducted extends Model
{
protected $casts = [
'id' => 'string',
'book_name' => 'string',
'date_inducted' => 'string',
];
protected $table = 'books';
protected $fillable = ['book_id', 'book_name', 'date_inducted', 'updated_at', 'created_at'];
}
查看:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Save test</title>
<link rel="stylesheet" href="/css/bootstrap-3.min.css">
<link rel="stylesheet" type="text/css" href="/css/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="/css/led.css">
<link rel="stylesheet" type="text/css" href="/css/jquery-ui.structure.min.css">
<link rel="stylesheet" type="text/css" href="/css/jquery-ui.theme.min.css">
<link rel="stylesheet" type="text/css" href="/css/dataTables.jqueryui.min.css">
<link rel="stylesheet" type="text/css" href="/css/ssstyle.css">
<script src="/js/jquery-1.12.4.js"></script>
<script src="/js/jquery.dataTables.min.js"></script>
<script src="/js/dataTables.jqueryui.min.js"></script>
<script src="/js/jquery-ui.min.js"></script>
<script src="/js/jquery-ui.accordion.multiple.js"></script>
<script src="/js/demo.js"></script>
</head>
<body>
<h3>ttot Save Test</h3>
<div>
<div class="containershe">
<table id="books" class="ttotsave" style="width:100%">
<thead>
<tr><th>ID</th>
<th>Name</th>
<th>Inducted</th>
{{--<th>Edit</th>--}}
{{--<th>Delete</th>--}}
</tr>
</thead>
</table>
</div>
<button data-token="{{ csrf_token() }}" type="submit">Submit</button>
<script type="text/javascript">
$(document).ready(function() {
oTable = $('#books').DataTable({
"processing": true,
"serverSide": true,
"scrollY": "500px",
"scrollX": true,
"scrollCollapse": true,
"paging": true,
"ajax": "{{ route('tabletotable.ttotgetposts') }}",
"dataSrc" : "",
"columns": [
{'data': 'id', render: function ( data, type, row)
{
if (data) {
var id = $('<div />').text(data).html();
}
else {
var id = '';
}
return '<input type="text" id="id" name="id[' + $('<div />').text(row.id).html() + ']" value=" ' + id + ' ">';
} },
{'data': 'book_name', render: function ( data, type, row)
{
if (data) {
var name = $('<div />').text(data).html();
}
else {
var name = '';
}
return '<input type="text" id="name" name="name[' + $('<div />').text(row.id).html() + ']" value=" ' + name + ' ">';
} },
{'data': 'date_inducted', render: function ( data, type, row)
{
if (data) {
var date = $('<div />').text(data).html();
}
else {
var date = '';
}
return '<input type="text" id="inducted" name="inducted[' + $('<div />').text(row.id).html() + ']" value=" ' + date + ' ">';
} },
]
});
$('button').click( function() {
var data = oTable.$('input, select').serialize();
$.ajax({
headers: {
'X-CSRF-TOKEN': $('button').data('token'),
},
type: "POST",
url: 'tabletotable',
dataSrc : 'data',
dataType: 'JSON',
data: data,
async: 'false',
success: function()
{
alert('saved');
console.log('my message' + response + data);
}
});
});
});
</script>
</div>
</body>
</html>
控制器:
<?php
namespace App\Http\Controllers;
use App\Books;
use App\Inducted;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use DataTables;
use DB;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function plunker()
{
return view('plunker');
}
public function ttotGetPosts()
{
$users = DB::table('books')->select('*');
return DataTables::of($users)
->make(true);
}
public function saveOrUpdate(Request $request){
dd($request->all());
DB::table('books')->where('id','=', $request->get('id'))
->update(['book_name' => $request->get('name'),
'date_inducted' => $request->get('inducted')]);
return redirect('/index')->with('status','Successfully updated !');
}
}
您需要的更多信息只是让我知道
亲切的问候
答案 0 :(得分:0)
试试这个
public function saveOrUpdate(Request $request){
// dd($request->all());
$ids = $request->get('id');
$names = $request->get('name');
$inducteds = $request->get('inducted')
foreach($ids as $id) {
DB::table('books')->where('id','=', $id)
->update([
'book_name' => $names[$id],
'date_inducted' => $inducteds[$id]
]);
}
return redirect('/index')->with('status','Successfully updated !');
}