【发布时间】:2019-10-15 02:22:20
【问题描述】:
当我尝试更新记录时,我得到一个 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);
}
});
}
【问题讨论】:
-
find($id)将返回 null 如果未找到具有给定 ID 的记录。检查id是否正确,如果不存在该id的记录,也可以使用findOrFail($id)抛出异常 -
如果你
dd($products)结果如何? -
你能分享一下迁移吗
-
findOrFail($id) 抛出“没有模型 [App\Product] 0 的查询结果”。我使用相同的 id 来删除它,它可以工作。 @porloscerrosΨ
-
Schema::create('products', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table-> unsignedBigInteger('category_id'); $table->string('description'); $table->decimal('price_neto', 10, 2); $table->decimal('iva', 10, 2); $table ->decimal('price_total', 10, 2); $table->timestamps(); }); @SherazKhan
标签: javascript php ajax laravel