Laravel|JQuery UI数据表可排序-错误500



我有edit.blade.php(艺术书视图(和内部照片列表(来自关系艺术书1:N照片中的多个上传表(:

<div class="dataTables_wrapper">
<div class="table-responsive">
<table class="table table-striped" id="table">
<thead>
...
</thead>
<tbody id="tablecontents">
@foreach(($artbook->photos) as $photo)
@if(!is_null($photo))
<tr class="row1" data-id="{{ $photo->order }}">
<td>{{ $photo->order }}</td>
<td><img class="img-responsive" src="/storage/photos/{{ $photo->filename }}" style="width:100%; height:auto;"/></td>
<td><a class="btn-dark btn-sm"><i class="fa fa-arrows my-handle" aria-hidden="true" style="color:#fff;"></i></a></td>
</tr>
@endif
@endforeach
</tbody>
</table>
</div>
</div>

在相同的视图中,我有脚本:

<script type="text/javascript">
$(function () {
$("#table").DataTable();
$( "#tablecontents" ).sortable({
items: "tr",
cursor: 'move',
opacity: 0.6,
update: function() {
sendOrderToServer();
}
});
function sendOrderToServer() {
var order = [];
$('tr.row1').each(function(index,element) {
order.push({
id: $(this).attr('data-id'),
position: index+1
});
});
$.ajax({
type: "POST", 
dataType: "json", 
url: "{{ url('artbook/edit/'.$artbook->id) }}",
data: {
order:order,
_token: '{{csrf_token()}}'
},
success: function(response) {
if (response.status == "success") {
console.log(response);
} else {
console.log(response);
}
}
});
}
});
</script>

我的ArtbookController的功能:

public function updatePhotoOrder(Request $request)
{
$photos = Photo::all();  
console.log('test');
foreach ($photos as $photo) {
$photo->timestamps = false; // To disable update_at field updation
$id = $photo->id;
foreach ($request->order as $order) {
if ($order['id'] == $id) {
$photo->update(['order' => $order['position']]);
}
}
}
return response('Update Successfully.', 200);
}

这是路线:

Route::post('artbook/edit/{id}','ArtbookController@updatePhotoOrder');

在模型中,我的订单字段是可以填写的,测试后我得到了一个错误:

localhost/artbook/edit/1 500


编辑:

[2020-09-29 21:53:58] local.ERROR: Use of undefined constant console - assumed 'console' (this will throw an Error in a future version of PHP) {"userId":1,"exception":"[object] (ErrorException(code: 0): Use of undefined constant console - assumed 'console' (this will throw an Error in a future version of PHP) at /public_html/app/Http/Controllers/ArtbookController.php:91)
[stacktrace]
#0 /public_html/app/Http/Controllers/ArtbookController.php(91): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Use of undefine...', '/home/fldd/doma...', 91, Array)
#1 [internal function]: App\Http\Controllers\ArtbookController->updatePhotoOrder(Object(Illuminate\Http\Request), '1')

有人能帮我吗?非常感谢!

好的,我解决了这个问题:更改此:

<tr class="row1" data-id="{{ $photo->order }}">

到此:

<tr class="row1" data-id="{{ $photo->id }}">

感谢lagbox的帮助!

最新更新