doujiu8145 2019-06-23 03:50
浏览 54
已采纳

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

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.

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.

展开全部

  • 写回答

1条回答 默认 最新

  • duanjiao3686 2019-06-24 08:33
    关注

    Found the fix thanks to Amirsadjad. This was the $fillable variable in the Todo model:

    protected $fillable=[
        'title',
    ];
    

    fix:

    protected $fillable=[
            'title',
            'status',
        ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部