简体   繁体   English

使用复选框更新Ajax DB不会在Laravel上更新

[英]Ajax DB update using checkbox not updating on Laravel

For some reason I'm not seeing an update to the DB even though there isnt any error. 由于某种原因,即使没有任何错误,我也看不到数据库的更新。

We're using Laravel in class, I tried using the exact same codes from different classmates and somehow it doesn't work for me. 我们在课堂上使用Laravel,我尝试使用来自不同同学的完全相同的代码,但对我来说不起作用。

index.blade.php: index.blade.php:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>This is your todo list</h1>
<ul>

@foreach($todos as $todo)

@if ($todo->status)
           <input type = 'checkbox' id ="{{$todo->id}}" checked>
       @else
           <input type = 'checkbox' id ="{{$todo->id}}">
       @endif
<li>
    <a href = "{{route('todos.edit',$todo->id)}}">{{$todo->title}} </a> 
</li>
@endforeach
</ul>
@can('manager')

<a href = "{{route('todos.create')}}">Add a new Todo </a>
@endcan

<script>
       $(document).ready(function(){
           $(":checkbox").click(function(event){
               $.ajax({
                   url:  "{{url('todos')}}" + '/' + event.target.id,
                   dataType: 'json',
                   type:  'put',
                   contentType: 'application/json',
                   data: JSON.stringify({'status':event.target.checked, _token:'{{csrf_token()}}'}),
                   processData: false,
                   success: function( data){
                        console.log(JSON.stringify( data ));
                   },
                   error: function(errorThrown ){
                       console.log( errorThrown );
                   }
               });               
           });
       });
   </script>
@endsection

update function in the controller file: 控制器文件中的更新功能:

    public function update(Request $request, $id)
   {
       //only if this todo belongs to user
       $todo = Todo::findOrFail($id);
      //employees are not allowed to change the title 
       if (Gate::denies('manager')) {
           if ($request->has('title'))
                  abort(403,"You are not allowed to edit todos..");
       }   

       //make sure the todo belongs to the logged in user
       if(!$todo->user->id == Auth::id()) return(redirect('todos'));

       //test if title is dirty

       $todo->update($request->except(['_token']));

       if($request->ajax()){
           return Response::json(array('result' => 'success1','status' => $request->status ), 200);
       } else {          
           return redirect('todos');           
       }
   }

nothing happens to the database, and no error message. 数据库没有任何反应,也没有错误消息。

Found the fix thanks to Amirsadjad. 感谢Amirsadjad找到了解决方法。 This was the $fillable variable in the Todo model: 这是Todo模型中的$ fillable变量:

protected $fillable=[
    'title',
];

fix: 固定:

protected $fillable=[
        'title',
        'status',
    ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM