0

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

symcbean
  • 47,736
  • 6
  • 59
  • 94
Vardana Bhanot
  • 94
  • 2
  • 11

2 Answers2

0

Better practice is to use method route() instead of url(). You should call your variable post. It's single element, but back to the main point. You validate wrong. In your request you don't have variables: title and body and Validator returns error before updating data.

Olek Szewczak
  • 23
  • 1
  • 1
  • 5
0

Maybe this will not work but check first:

  • Your route must be "PUT"
  • Will be better if you in update use $post instead of $posts. You are requesting only one post ;)
  • In view use named routes instead of urls. In future will be better to manage everything without searching where you used it :)
  • Change $posts->update(); into:

    if ($post->isDirt()) {
        $post->save(); //Notice that here i am using save instead of update
    }
    

Could you please provide to us your request from ajax? I am what body you are sending? If you are sending files you need to use multipartdata form. Look here: What does enctype='multipart/form-data' mean?