1
路線無法更新數據
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>
阿賈克斯
$('#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,描述和userfile的
當我點擊提交,我得到的數據是這樣
我無法更新數據,當我嘗試更新他們,我的數據沒有改變。 我不知道爲什麼,我覺得我沒有做錯事在我的代碼。請幫我解決這個問題,如果你知道答案
我試過了,但是沒有工作..我在StackOverFlow中讀了很多問題,他們說表格數據只能在POST方法中使用,並且不能在PUT方法中使用。你有什麼主意嗎? –