Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Laravel 5.8从空值创建默认对象_Javascript_Php_Ajax_Laravel - Fatal编程技术网

Javascript Laravel 5.8从空值创建默认对象

Javascript Laravel 5.8从空值创建默认对象,javascript,php,ajax,laravel,Javascript,Php,Ajax,Laravel,当我尝试更新一条记录时,我从空值中得到一个ErrorException创建默认对象,并指向我的控制器$products->name=$request->input'name'行 产品控制器 公共函数updateRequest$request$id { $products=产品::查找$id; $products->name=$request->输入'name'; $products->category_id=$request->选择'category_id'; $products->descr

当我尝试更新一条记录时,我从空值中得到一个ErrorException创建默认对象,并指向我的控制器$products->name=$request->input'name'行

产品控制器

公共函数updateRequest$request$id { $products=产品::查找$id; $products->name=$request->输入'name'; $products->category_id=$request->选择'category_id'; $products->description=$request->输入'description'; $products->price_neto=$request->输入'price_neto'; $products->iva=$request->input'iva'; $products->price_total=$request->输入'price_total'; $products->save; 返回响应->json[ 'error'=>false, “产品”=>$products, ], 200;
} 查询返回空值时发生此错误

您可以尝试以下方法:

public function update(Request $request, $id)
{
    $products = Product::find($id);

    if($products instanceof Product){
        $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);
    }
    return response()->json([
        'error' => true,
        'message'  => 'Product not found.',
    ], 404);
}

您缺少将产品id的值分配给您从中获取产品id以发送put请求的输入:

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);
        }
    });
}
$("#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();
        }
    });
});
然后,您可以让它为put请求构建ajax url:

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);
        }
    });
}
$("#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();
        }
    });
});

如果未使用给定id查找记录,则find$id将返回null。请检查id是否正确,如果该id不存在记录,您也可以使用FindDorFail$id引发异常。如果您添加了$products,结果是什么?您能否共享MigrationFindDorFail$id引发的“模型[App\Product]0没有查询结果”吗。我使用相同的id来删除它,它可以正常工作@porloscerrosψ模式::创建“产品”,函数蓝图$table{$table->big递增'id';$table->string'name';$table->unsignedbiginger'category_id';$table->string'description';$table->十进制'price_neto',10,2;$table->十进制'iva',10,2;$table->十进制'price_total',10,2;$table->时间戳;}@SherazKhanYes抛出“找不到产品”。@AkshayJoshib因为您的表中的产品不可用,您正在传入控制器$id。谢谢!是的,就是这样。我忘记了我的产品id,我只需要在控制器中将$products->category\U id=$request->选择“category\U id”更改为$products->CATEGORES\U id=$request->input'category\u id'太棒了!很高兴知道你解决了它。我在控制器上错过了它,是的,你可以使用该语法或$request->category\u id谢谢你,非常感谢你。你救了我。