How to fix: 'Creating default object from empty value' in Laravel? [closed]

1

On the blade I have this code:

@foreach
<tr id="{{$employee->id}}">
<td class="visibleemployee">
<form action="{{route('admin.employees.cambiarVisible',$employee->id)}}">
  <button type="button" id="buttonchangevisible" data-id="{{$employee->id}}">
    @if ($employee->public == '1')
        <i class="fa fa-check" aria-hidden="true" id="margindataemployee" class="cambiarsiporno"></i>
    @else
        <i class="fa fa-times" aria-hidden="true" id="margindataemployee"></i>
    @endif
  </button>
  <input type="hidden" name="_token" value="{{Session::token()}}">
 </form>
 </td>
 </tr>

When you click on the button, this function is executed with Ajax:

$("#buttonchangevisible").click(function(e){
        e.preventDefault();
        var button = $(this);
        var id = button.data('id');
        var formData = new FormData($(this)[0]);
        $.ajax({
            url:'employee/cambiarVisible/' + id,
            type: 'PUT',
            data: formData,
            success: function() {
                location.reload();
            },
            cache: false,
            contentType: false,
            processData: false
        });
        return false;
    });

Calling the following controller method:

public function cambiarVisible(Request $request, $id)
    {
        $employee = Worker::findOrFail($id);
        $employee->public = ($employee->public == 1) ? 0 : 1;
        $employee->save();
    }

File routes: (everything goes inside a group that adds the prefix admin).

  Route::put('employee/cambiarVisible/{id}', ['uses' => 'AdminController@cambiarVisible', 'as' => 'admin.employees.cambiarVisible']);

The error cited in the title is solved, now clicking on the first row makes the call correctly but does not update the field. Clicking on any other row directly does not make the call.

    
asked by Lluís Puig Ferrer 30.10.2017 в 11:30
source

1 answer

1

The Ajax code path is misplaced, you can not use laravel routes in js ('employee / cambiarVisible / {id} '). If you do a "dd ($ id)" in the controller you will see that the value does not arrive, so you will have to pass it through POST / PUT

I'll give you an example with the type delete (for PUT it would only change the _method from type delete to put)

function eliminar(ruta) {
 $.ajax({
                url: ruta,
                data: {'_method': 'delete', '_token': '{{Session::token()}}'},
                type: 'post',
                success: function () {
                    location.reload();
                },
                error: function () {
                    swal({
                        title: '¡¡ERROR!!',
                        type: 'error',
                        text: "No se pudo eliminar",
                        confirmButtonText: 'CONTINUAR',
                        showCancelButton: false
                    });
                }
            });
}
    
answered by 30.10.2017 / 12:21
source