I am trying to update the value of a column but its now working. I am trying this to update views on the page but its not happening. There is no error Still its not updating in the database. View page
<form style="" name="fbCommentCountform" id="fbCommentCountForm" action="{{ url('/posts/{$display_post->id}')}}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">
<input type="text" name="commentCount" id="fbFormCommentCount">
<input type="text" name="visitCount" id="hiddenFormPostVisitCounter" value="{{ $display_post->visit_count }}">
</form>
Controller
public function update(Request $request, $id)
{
$image = $request->file('featured_img');
$posts = Post::findOrFail($id);
if(!empty($image))
{
$name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/public/images');
$imagePath = $destinationPath. "/". $name;
$image->move($destinationPath, $name);
$posts->featured_img = $name;
}
$posts->title = $request->title;
$posts->body = $request->body;
$posts->slug = $request->slug;
$posts->categories_id = $request->category;
$posts->description = $request->description;
$posts->visit_count = $request->visitCount;
$this->validate($request,[
'title' => 'required|string|max:255',
'body' => 'required'
]);
$posts->update();
return redirect('posts');
Now here is the ajax Code
<script>
let fbCommentCount = document.getElementById('fbCommentCount').getElementsByClassName('fb_comments_count');
setTimeout(function() {
document.getElementById('fbFormCommentCount').value = fbCommentCount[0].innerHTML;
var $formVar = $('#fbCommentCountForm');
let visitCount = document.getElementById('hiddenFormPostVisitCounter').value;
let visitCountPlusOne = parseInt(visitCount) + 1;
document.getElementById('hiddenFormPostVisitCounter').value = visitCountPlusOne;
$.ajax({
url: $formVar.prop('{{ url('/posts/{$display_post->id}') }}'),
method: 'PUT',
data: $formVar.serialize(),
});
}, 1000);
</script>
If someone could help me to solve this problem