【问题标题】:Can not update the data无法更新数据
【发布时间】:2018-02-22 22:16:30
【问题描述】:

路线

Route::resource('/mediafile', '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>

阿贾克斯

$('#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

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

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

【问题讨论】:

    标签: php jquery ajax html laravel-5


    【解决方案1】:
    <form  name="itemGroupForm" action="{{URL::to('editItem',$items[0]->id)}}"   id="itemGroup" data-parsley-validate="" method="post"  onsubmit="return submitFormItemGroup();">
    
         function submitFormItemGroup() {
      var form_data = new FormData(document.getElementById("itemGroup"));
      form_data.append("label", "WEBUPLOAD");
      $.ajax({
          url: "{{URL::to('editItem',$items[0]->id)}}}",
          type: "POST",
          data: form_data,
          processData: false,  // tell jQuery not to process the data
          contentType: false   // tell jQuery not to set contentType
      }).
    

    【讨论】:

    • 我试过了,但是没有用。。我在 StackOverFlow 中阅读了很多问题,他们说表单数据只能用于 POST 方法,不能用于 PUT 方法。你有什么想法吗?
    【解决方案2】:

    将 PUT 路由更改为 POST 路由

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

    由于表单数据只能在POST方法中使用,所以如果要更新图片,必须使用POST方法。

    【讨论】:

      猜你喜欢
      • 2013-11-07
      • 1970-01-01
      • 2021-09-24
      • 2020-09-26
      • 2020-02-19
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多