Обновление Ajax DB с помощью флажка не обновляется на Laravel - PullRequest
1 голос
/ 23 июня 2019

По какой-то причине я не вижу обновления в БД, хотя ошибки нет.

Мы используем Laravel в классе, я пытался использовать одинаковые коды разных одноклассников, и почему-то у меня это не работает.

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

функция обновления в файле контроллера:

    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');           
       }
   }

с базой данных ничего не происходит, и сообщение об ошибке отсутствует.

1 Ответ

0 голосов
/ 24 июня 2019

Нашли исправление благодаря Амирсаджаду.Это была переменная $ fillable в модели Todo:

protected $fillable=[
    'title',
];

fix:

protected $fillable=[
        'title',
        'status',
    ];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...