在laravel中使用ajax保存文件更改

时间:2017-11-16 00:12:20

标签: php ajax laravel

我想从索引页面更改数据的某些部分,而不使用Ajax加载编辑页面。 例如,我的评论有status,其中值为01以及comment部分。我想从索引页面更改这些值。

image

我所拥有的所有代码只是我的索引方法,我加载我的列表

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

我需要帮助才能将我的索引页面作为带有Ajax的表单进行编辑,但不要使用我的Update方法,因为我需要edit.blade.php(也许在我的控制器中添加另一种方法?)

感谢。

1 个答案:

答案 0 :(得分:1)

首先,您需要一个更新帖子状态的控制器。正如所讨论的那样,完全取决于你想要做什么。

我的建议是创建一个名为UpdatePostStatusController的新控制器,然后使用更新方法获取post id和状态1或0并更新它。

你的索引文件应该有每次更改状态时都会被触发的javascript

复选框html

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

现在你的ajax应该

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

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

//do your ajax post request here here
}