1

I'm using laravel and trying to save data using post through ajax but data is not saved in database. I'm getting following error: jquery.min.js:2 POST http://localhost:8000/admin/products/attributes/add 500 (Internal Server Error). My code is as follows:

view:

<script>
     $("#add_attributes_info").click(function(e){
        e.preventDefault(); 
        $.ajax({
            type: "POST",
            url: '/admin/products/attributes/add',
            data: $('#frmattributes').serialize(),
            success: function(msg) {
                console.log('success'+msg);
              }
        });
        });
</script>
 <form action="#" id="frmattributes" method="POST">
            <h3 class="tile-title">Add Attributes To Product</h3>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="values">Select an value <span class="m-l-5 text-danger"> *</span></label>
                        <select id="attribute_values" name="value" class="form-control custom-select mt-15">

                        </select>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label class="control-label" for="quantity">Quantity</label>
                        <input class="form-control" name="quantity" type="number" id="quantity"/>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label class="control-label" for="price">Price</label>
                        <input class="form-control" name="price" type="text" id="price"/>
                        <small class="text-danger">This price will be added to the main price of product on frontend.</small>
                    </div>
                </div>
                <div class="col-md-12">
                    <button class="btn btn-sm btn-primary" id="add_attributes_info">
                        <i class="fa fa-plus"></i> Add
                    </button>
                </div>
            </div>
            </form>

Controller:

  public function addAttribute(Request $request)
    {
        $productAttribute = ProductAttribute::create($request->data);

        if ($productAttribute) {
            return response()->json(['message' => 'Product attribute added successfully.']);
        } else {
            return response()->json(['message' => 'Something went wrong while submitting product attribute.']);
        }
    }
2
  • Check your storage/logs for the .log file that contains a full error, rather then just 500. Commented Jan 5, 2020 at 12:07
  • @nakov: In log i'm getting this Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given, Commented Jan 5, 2020 at 12:12

4 Answers 4

1

You should use:

$productAttribute = ProductAttribute::create($request->all());

However you should keep in mind this is very risky without validation.

You should add input validation and then use:

$productAttribute = ProductAttribute::create($request->validated());
Sign up to request clarification or add additional context in comments.

Comments

1

Use $request->all();

  public function addAttribute(Request $request)
    {
        $productAttribute = ProductAttribute::create($request->all());

        if ($productAttribute) {
            return response()->json(['message' => 'Product attribute added successfully.']);
        } else {
            return response()->json(['message' => 'Something went wrong while submitting product attribute.']);
        }
    }

Comments

1

PS : I made some changes to get it works

Hope this help

<head>

    <title></title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
        $.ajaxSetup({

            headers: {

                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

            }

        });

        function submitForm() {
            $.ajax({
                type: "POST",
                url: '../admin/products/attributes/add',
                data: $('#frmattributes').serialize(),
                success: function(msg) {
                    console.log('success' + msg);
                }
            });
        }
    </script>
</head>

<body>

    <form id="frmattributes">
        <h3 class="tile-title">Add Attributes To Product</h3>
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label for="values">Select an value <span class="m-l-5 text-danger"> *</span></label>
                    <select id="attribute_values" name="value" class="form-control custom-select mt-15">

                    </select>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label class="control-label" for="quantity">Quantity</label>
                    <input class="form-control" name="quantity" type="number" id="quantity" />
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label class="control-label" for="price">Price</label>
                    <input class="form-control" name="price" type="text" id="price" />
                    <small class="text-danger">This price will be added to the main price of product on frontend.</small>
                </div>
            </div>
            <div class="col-md-12">
                <button class="btn btn-sm btn-primary" id="add_attributes_info" type="button" onclick="submitForm()">
                    <i class="fa fa-plus"></i> Add
                </button>
            </div>
        </div>
    </form>
</body>

</html>

Comments

0

So in the controller, change the $request->data with :

$productAttribute = ProductAttribute::create($request->all());

or also check what the request contains, before creating you can check using:

dd($request->all());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.