0

I am trying to send ajax post request to my controller so that customer data would be save in database. I show you guys my controller method and ajax request.

My controller method is given below:

 public function Verify(Request $request) {

            $this->validate($request, [
                'first_name' => 'required',
                'middle_name' => 'required',
                'last_name' => 'required',
                'email' => 'email:rfc,dns',
                'areacode' => 'required|digits:2',
                'phonenumber' => 'required|digits:7',
                'streetaddress' => 'required',
                'streetaddressline_2' => 'required',
                'city' => 'required',
                'province' => 'required',
                'postcode' => 'required',
                'country' => 'required',
            ]);

            $input = $request->all();

            $customer = new Customer();
            $customer->name = $input['first_name'] . ' ' . $input['middle_name'] . ' ' . $input['last_name'];
            $customer->email = $input['email'];
            $customer->contactno = $input['areacode'] . $input['phonenumber'];
            $customer->streetaddress = $input['streetaddress'];
            $customer->streetaddressline_2 = $input['streetaddressline_2'];
            $customer->city = $input['city'];
            $customer->province = $input['province'];
            $customer->postcode = $input['postcode'];
            $customer->country = $input['country'];

            $customer->save();
             $response = array(

            'status' => 'success',
            'msg'    => 'customer created successfully',
        );

        return Response::json_encode($response);
        }
}

And this is my ajax request

var customer_data = {
        first_name: first_name,
        middle_name: middle_name,
        last_name: last_name,
        email: email,
        areacode: areacode,
        phonenumber: phonenumber,
        streetaddress: streetaddress,
        streetaddressline_2: streetaddressline_2,
        city: city,
        province: province,
        postcode: postcode,
        country: country
    };


    if (
            first_name &&
            middle_name &&
            last_name &&
            email &&
            areacode &&
            phonenumber &&
            streetaddress &&
            streetaddressline_2 &&
            city &&
            province &&
            postcode &&
            country
            ) {

        $.ajax('/verifydata',
                {
                    method: 'POST',
                    dataType: 'json', // type of response data
                    data: customer_data,
                    success: function (data) {   // success callback function
                        console.log('success: '+data);
                    },
                    error: function (data) { // error callback 
                       var errors = data.responseJSON;
                       console.log(errors);


                    }
                });
        $("#form1").show();

    }

When I send post ajax request to my controller, its gives me the error of

{message: "Array to string conversion", exception: "ErrorException",…}
exception: "ErrorException"
file: "/home/ew3tfqy9/public_html/vendor/laravel/framework/src/Illuminate/Support/Str.php"
line: 449
message: "Array to string conversion"

I have no idea whats wrong with this code. Any help would be appreciated ... Thank you ... !!!

8
  • Can you please also tell us the datatype you used to save in DB ? Commented Jun 9, 2020 at 6:09
  • @Vipertecpro datatype of what? Commented Jun 9, 2020 at 7:26
  • datatype of anything which you supposed to take longText but taken varchar in database, or maybe while saving data into database you supposed to put json_encode($whateverLongString) ? Commented Jun 10, 2020 at 19:25
  • @Vipertecpro here is my customer migration that I want to store in database through this ajax request that is mention in above question migration is : $table->id(); $table->string('name'); $table->string('email'); $table->integer('contactno'); $table->text('streetaddress'); $table->text('streetaddressline_2')->nullable(); $table->string('city'); $table->string('province'); $table->integer('postcode'); $table->string('country'); $table->timestamps(); Commented Jun 11, 2020 at 3:31
  • Have you checked what "ErrorException" says ?, Your migration is fine, can you please update question with full ErrorException message, and i'm sure you'll find problem in ErrorException message, pluse please put your code into try catch block Commented Jun 11, 2020 at 7:46

1 Answer 1

1

I detect the issue and it was the wrong response I return from controller to ajax. so I change it and it is given below

$response = array(

            'status' => 'success',
            'msg'    => 'customer created successfully',
        );

        return json_encode($response);
        }
Sign up to request clarification or add additional context in comments.

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.