Naveen Kumar
Naveen Kumar

Reputation: 1626

Returning JSON response from controller in laravel

I am new to Javascript. I want to do a ajax updation in my form. Everything works fine. once the update is done the value is updated in the database. where the problem occurs is when i try to update it in the view. Here is my controller

public function updatePost(Request $request)
{

    $post = $request->all();
    $val = \Validator::make($request->all(), [

            'client_id' => 'required',
            'client_name' => 'required',
            'client_business' => 'required',
            'client_ref' => 'required',
            'gmail_mail' => 'required',
            'gmail_pass' => 'required',
            'client_dob' => 'required',
            'client_addr' => 'required',
            'client_no1' => 'required',
            'client_ref' => 'required',
            'domain_name' => 'required',
            'domain_p_date' => 'required',
            'domain_reg' => 'required',
            'domain_ex_date' => 'required',
            'domain_acc_email' => 'required',
            'domain_acc_pass' =>  'required',
            'domain_id' => 'required',

        ]);

            if ($val->fails()) {

            return redirect()->back()->withErrors($val->errors());
        }


             else {

                $data = array(

                'client_id' => $post['client_id'],
                'client_name' => $post['client_name'],
                'client_business' => $post['client_business'],
                'client_ref' => $post['client_ref'],
                'gmail_mail' =>  $post['gmail_mail'],
                'gmail_pass'=> $post['gmail_pass'],
                'client_dob'=> $post['client_dob'],
                'client_addr'=> $post['client_addr'],
                'client_no1'=> $post['client_no1'],

                'domain_name' => $post['domain_name'],
                'domain_p_date' => $post['domain_p_date'],

                'domain_reg' => $post['domain_reg'],
                'domain_ex_date' => $post['domain_ex_date'],
                'domain_acc_email' => $post['domain_acc_email'],
                'domain_acc_pass' => $post['domain_acc_pass'],
                'domain_id' => $post['domain_id'],

                );

            //var_dump($data);

           $update_data = domain_details::where('domain_id', $post['domain_id'])
           ->update($data);

                if ($update_data) {

                    $new_data = domain_details::where('domain_id',$post['domain_id'])->get();

                    return response()->json( ['client_id'=> 'johncena'],200);
                }
                else
                {

                    return redirect()->back()->withErrors($val->errors());
                }



        }

this is the data i receive from the form and update. Updation on the Databse works fine. This is where the problem occurs

if ($update_data) {

$new_data = domain_details::where('domain_id',$post['domain_id'])->get();

return response()->json( ['client_id'=> $new_data->client_id],200);

json( ['client_id'=> $new_data->client_id],200); if i pass just a string it works fine like json( ['client_id'=> 'John Doe'],200);.

The other problem is when i see the console. If i try to update the form without changing anything it throws a 500 internal server error. Here is my Javascript code that handles the response from the controller. Sometime the string is changed to John Doe and most of the times i get 500 internal Server error.

$('#update-modal').on('click',function(){

$.ajax({

    type : "get",
    url : updateURL,
    data : { client_id : $('#client_id').val(),
             client_name : $('#client_name').val(),
             client_business : $('#client_business').val(),
             client_ref : $('#client_ref').val(),
             gmail_mail : $('#gmail_mail').val(),
             gmail_pass : $('#gmail_pass').val(),
             client_dob : $('#client_dob').val(),
             client_addr : $('#client_addr').val(),
             client_no1 : $('#client_no1').val(),

             domain_name : $('#domain_name').val(),
             domain_p_date : $('#domain_p_date').val(),
             domain_reg : $('#domain_reg').val(),
             domain_ex_date : $('#domain_ex_date').val(),
             domain_acc_email : $('#domain_acc_email').val(),
             domain_acc_pass :  $('#domain_acc_pass').val(),
             domain_id : domain_id,
             _token : token 
         }

})
.done(function(msg){
    $(client_id_element).text(msg['client_id']);



});
});

I know i am missing something. I cant figure out what it is. Thanks in advance

Upvotes: 1

Views: 1607

Answers (1)

Khallister
Khallister

Reputation: 266

Well, I think that Eloquent get() returns a collection, not a certain object. Looks like you want to return just one object with domain_id that is unique. Try using

$new_data = domain_details::where('domain_id',$post['domain_id'])->first();

instead.

Upvotes: 2

Related Questions