mafortis
mafortis

Reputation: 7138

Save fileds changes with ajax in laravel

I want to change some parts of my data from index page without loading edit page with Ajax. For example my reviews has status where value is 0 or 1 and also comment section. I want to change these values from my index page.

image

All the code i have is simply my index method only, where i load my list

public function index()
    {
      $ratings = Rating::orderby('id', 'desc')->paginate(10);
      return view('admin.ratings.index', compact('ratings'));
    }

I need help to make my index page as a form with Ajax in order to edit from there but not to use my Update method because I'll need it for my edit.blade.php (maybe add another method in my controller?)

Thanks.

Upvotes: 0

Views: 156

Answers (1)

usrNotFound
usrNotFound

Reputation: 2830

First of all you need a controller that updates the post status. As discussed it totally up to you want you want to do.

My suggestion is to create a new controller called UpdatePostStatusController and then have update method that takes post id and the status either 1 or 0 and update it.

Your index file should have javascript that gets triggred everytime you change the status like so

checkbox html

<input type="checkbox" data-uuid="{{ $post->id }}" class="update" @if($post->status) checked @endif/>

Now your ajax should

 $('.update').change(function() {

  data = {'status' : this.checked }
  post_id =  $(this).data('uuid');

//do your ajax post request here here
}

Upvotes: 1

Related Questions