mercredi 13 septembre 2017

Can not update the data

Route

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

}

View

<form class="form-horizontal" id="media-form" enctype="multipart/form-data">
    
        <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="">
              @else
                <input type="text" id="medianame" class="form-control" name="MediaName">
              @endif
                @if ($errors->has('MediaName'))
                <span class="help-block">
                    <strong></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"></textarea>
                @else
            <textarea class="form-control" name="Description" style="height: 200px"></textarea>
            @endif
                @if ($errors->has('Description'))
                <span class="help-block">
                    <strong></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="" style="height: 150px;margin-left: 10px">
            <textarea readonly="" class="select valid" style="height:30px; width: 100%; margin-top: 10px"></textarea>
                @endif
            <input type="file" name="UserFile">
            </div>
        </div>

        <input type="hidden" name="_token" value=""></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;

});

[I put the MediaName, Description, and UserFile][1]

[1]: http://ift.tt/2x3cSxh

[when I clicked submit, I got data like this][2]

[2]: http://ift.tt/2wrf7qn

I can not update the data, when I try to update them, my data have not changed. I don't know why, I think that I did not do something wrong in my code.. Please help me to solve this if you know the answer



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire