无法更新数据

时间:2017-09-14 04:20:03

标签: php jquery ajax html5 laravel-5

路线

Route::resource('/mediafile', 'MediaController');

的MediaController

public function update(Request $request, $id)
{
    $media = Media::findOrFail($id);
    if($request->hasFile('UserFile')) {
        $image = $request->file('UserFile');
        $filename = $image->getClientOriginalName();  
        Image::make($image)->resize(300, 300)->save(public_path('media/' . $filename));
        $media->MediaPath = $filename;
        $media->MediaName = $filename;
        $media->Description = $request->Description ? $request->Description : '';
        $media->save();
    }

    return response()->json($media);

}

查看

<form class="form-horizontal" id="media-form" enctype="multipart/form-data">
    {{ csrf_field() }}
        <div class="form-group">
            <div class="col-xs-6 col-sm-6 col-md-6">
                <label for="MediaName">Nama Media</label>
              @if($edit)
              <input type="text" id="medianame" class="form-control" name="MediaName" value="{{$mediaEdit != null ? $mediaEdit->MediaName : ''}}">
              @else
                <input type="text" id="medianame" class="form-control" name="MediaName">
              @endif
                @if ($errors->has('MediaName'))
                <span class="help-block">
                    <strong>{{ $errors->first('MediaName') }}</strong>
                </span>
                @endif
            </div>
        </div>

        <div class="form-group">
            <div class="col-xs-6 col-sm-6 col-md-6">
                <label for="Description">Description</label>
            @if($edit)
            <textarea class="form-control" name="Description" style="height: 200px">{{$mediaEdit->Description}}</textarea>
                @else
            <textarea class="form-control" name="Description" style="height: 200px"></textarea>
            @endif
                @if ($errors->has('Description'))
                <span class="help-block">
                    <strong>{{ $errors->first('Description') }}</strong>
                </span>
                @endif
            </div>
        </div>

        <div class="form-group">
            <div class="col-xs-6 col-sm-6 col-md-6">
                <label for="Gambar">Gambar</label>
            @if($edit)
            <img src="{{ asset('media/' . $mediaEdit->MediaPath) }}" style="height: 150px;margin-left: 10px">
            <textarea readonly="" class="select valid" style="height:30px; width: 100%; margin-top: 10px">{{ asset('media/' . $mediaEdit->MediaPath) }}</textarea>
                @endif
            <input type="file" name="UserFile">
            </div>
        </div>

        <input type="hidden" name="_token" value="{{ csrf_token() }}"></input>
        <input type="hidden" name="_method" value="post"></input>
        <button type="submit" class="btn btn-info">Submit</button>

    </form>

的Ajax

$('#media-form').submit(function(){
var formData = new FormData(this);
swal({
  title: 'Are you sure?', 
  type: 'info',
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Confirm!",
  closeOnConfirm: false,
  closeOnCancel: false
  },
    //function
    function(isConfirm){
      if(isConfirm){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
          type: "<?php echo $actionMethod; ?>",
          url: "<?php echo $actionURL; ?>",
          data: formData,
          dataType: 'json',
          processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
        })
        .done(function(data){
          if(data.id){
              swal({
                title: "Saved!",
                text: "Your Category has been saved.",
                type: "success"}, function(){
                  window.location.href = "<?php echo url('mediafile'); ?>";
              });
            }else{
              swal("Try again");
            }
            console.log(data);
        })
        .error(function(data){
            swal("Cancelled", "Please fill the data first.");
            console.log('Error:', data);
        });
      } else{
        swal("Cancelled");
      }
    //end function
});
return false;
});

我放了MediaName,Description和UserFile

enter image description here

当我点击提交时,我得到了这样的数据

enter image description here

我无法更新数据,当我尝试更新数据时,我的数据没有改变。 我不知道为什么,我认为我的代码没有做错。如果你知道答案,请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson (list));

答案 1 :(得分:0)

将PUT路由更改为POST路由

Route::resource('/mediafile', 'MediaController', ['except' => ['update']]);
Route::post('/mediafile/{id}', 'MediaController@update');

因为表单数据只能在方法POST中使用,所以如果你想更新你的图片,你必须使用POST方法。