i have edit.blade.php (artbook view) and inside list of photos (from multiple upload - tables in relationship artbooks 1 : N photos):
<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>
in the same view I have script:
<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>
Function of my 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);
}
and here is route:
Route::post('artbook/edit/{id}','ArtbookController@updatePhotoOrder');
in model my Order field is fillable, and after testing I get an error:
localhost/artbook/edit/1 500
Edit:
[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')
Can someone help me? So much thanks!