当我尝试更新记录时,出现ErrorException“从空值创建默认对象”,并指向我的控制器的$ products-> name = $ request-> input('name')行。
产品控制器
public function update(Request $request, $id)
{
$products = Product::find($id);
$products->name = $request->input('name');
$products->category_id = $request->select('category_id');
$products->description = $request->input('description');
$products->price_neto = $request->input('price_neto');
$products->iva = $request->input('iva');
$products->price_total = $request->input('price_total');
$products->save();
return response()->json([
'error' => false,
'products' => $products,
], 200);
}
我已经搜索过,但仍然找不到解决方案。
product.blade.php
@foreach ($product as $products)
<tr>
<td>{{$products->id}}</td>
<td>{{$products->name}}</td>
<td>{{$products->category_id}}</td>
<td>{{$products->description}}</td>
<td>{{$products->price_neto}}</td>
<td>{{$products->iva}}</td>
<td>{{$products->price_total}}</td>
<div class="form-button-action">
<a onclick="event.preventDefault();editProductForm({{$products->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$products->id}}"><i class="material-icons" data-toggle="tooltip" title="Editar"></i></a>
<a onclick="event.preventDefault();deleteProductForm({{$products->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Eliminar"></i></a>
</div>
</td>
</tr>
@endforeach
路线
这些是我的路线。
Route::group(['prefix' => 'product'], function () {
Route::get('/{id}', [
'uses' => 'ProductController@show',
'as' => 'product.show',
]);
Route::post('/', [
'uses' => 'ProductController@store',
'as' => 'product.store',
]);
Route::put('/{id}', [
'uses' => 'ProductController@update',
'as' => 'product.update',
]);
Route::delete('/{id}', [
'uses' => 'ProductController@destroy',
'as' => 'product.destroy',
]);
});
Product.js
这就是我的模型通过按下“编辑”按钮的方式
$("#btn-edit").click(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'PUT',
url: '/product/' + $("#frmEditProduct input[name=product_id]").val(),
data: {
name: $("#frmEditProduct input[name=name]").val(),
category_id: $("#frmEditProduct select[name=category_id]").val(),
description: $("#frmEditProduct input[name=description]").val(),
price_neto: $("#frmEditProduct input[name=price_neto]").val(),
iva: $("#frmEditProduct input[name=iva]").val(),
price_total: $("#frmEditProduct input[name=price_total]").val(),
image: $("#frmEditProduct input[name=image]").val(),
},
dataType: 'json',
success: function(data) {
$('#frmEditProduct').trigger("reset");
$("#frmEditProduct .close").click();
window.location.reload();
},
error: function(data) {
var errors = $.parseJSON(data.responseText);
$('#edit-product-errors').html('');
$.each(errors.messages, function(key, value) {
$('#edit-product-errors').append('<li>' + value + '</li>');
});
$("#edit-error-bag").show();
}
});
});
An image on the project when I press Edit button
function editProductForm(product_id) {
$.ajax({
type: 'GET',
url: '/product/' + product_id,
success: function(data) {
$("#edit-error-bag").hide();
$("#frmEditProduct input[name=name]").val(data.products.name);
$("#frmEditProduct select[name=category_id]").val(data.products.category_id);
$("#frmEditProduct input[name=description]").val(data.products.description);
$("#frmEditProduct input[name=price_neto]").val(data.products.price_neto);
$("#frmEditProduct input[name=iva]").val(data.products.iva);
$("#frmEditProduct input[name=price_total]").val(data.products.price_total);
$('#editProductModal').modal('show');
},
error: function(data) {
console.log(data);
}
});
}
最佳答案
您缺少将产品ID的值分配给从中发送看跌期权的输入的输入:
function editProductForm(product_id) {
$.ajax({
type: 'GET',
url: '/product/' + product_id,
success: function(data) {
$("#edit-error-bag").hide();
$("#frmEditProduct input[name=product_id]").val(product_id) // <- assign the new value here!!!
$("#frmEditProduct input[name=name]").val(data.products.name);
$("#frmEditProduct select[name=category_id]").val(data.products.category_id);
$("#frmEditProduct input[name=description]").val(data.products.description);
$("#frmEditProduct input[name=price_neto]").val(data.products.price_neto);
$("#frmEditProduct input[name=iva]").val(data.products.iva);
$("#frmEditProduct input[name=price_total]").val(data.products.price_total);
$('#editProductModal').modal('show');
},
error: function(data) {
console.log(data);
}
});
}
那么您可以获取它来为put请求构建ajax网址:
$("#btn-edit").click(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'PUT',
url: '/product/' + $("#frmEditProduct input[name=product_id]").val(), // <- here you get the input value!!!
data: {
name: $("#frmEditProduct input[name=name]").val(),
category_id: $("#frmEditProduct select[name=category_id]").val(),
description: $("#frmEditProduct input[name=description]").val(),
price_neto: $("#frmEditProduct input[name=price_neto]").val(),
iva: $("#frmEditProduct input[name=iva]").val(),
price_total: $("#frmEditProduct input[name=price_total]").val(),
image: $("#frmEditProduct input[name=image]").val(),
},
dataType: 'json',
success: function(data) {
$('#frmEditProduct').trigger("reset");
$("#frmEditProduct .close").click();
window.location.reload();
},
error: function(data) {
var errors = $.parseJSON(data.responseText);
$('#edit-product-errors').html('');
$.each(errors.messages, function(key, value) {
$('#edit-product-errors').append('<li>' + value + '</li>');
});
$("#edit-error-bag").show();
}
});
});
关于javascript - Laravel 5.8从空值创建默认对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58370193/