mardi 25 octobre 2016

POST 500 (Internal Server Error) - Laravel and Ajax

I want to send post request with ajax to controller in laravel. The ajax request send two input arguments and I want controller to find the column in the database with the first argument and then to set the name attribute with the second input argument. But I have this error message Creating default object from empty value

Ajax function:

$('#saveUserProfile').on('click', function () {

                var $finduser = $('input[name=findUser]').val();  
                var $name = $('input[name=userprofilename]').val();


                    $.ajax({
                        type:"POST",
                        url:'/code/task1/public/updateUser',
                        data: {
                             'name' : $name,
                             'finduser' : $finduser,
                            // 'email' : $email,
                         },
                        success:function(data){

                            $("#input1").val(data[0].name);

                        }

                    });
         }); 

and the function in my controller

public function updateUser(Request $request){

        $return_array = array();

        $findUserInput = $request->get('finduser');

        $user = User::where('name',$findUserInput) -> first();

        $user->name = $request->get('name');

        $user->save();

        $data =  DB::select("SELECT * FROM users where name='$findUserInput'");


        if(count($data) > 0){
            foreach($data as $da){
                $return_array[] = $da;
            } 
        }

        return $return_array;

    }

Any ideas?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2e6JLwN
via IFTTT

1 commentaire:

  1. I have the same problem, using laravel 5.3. I've noticed if I dont try to get any arguments in the controller, the controller gets called and the success result is fired back to the ajax, when using Input::get(), $request->get(''), $request->input(), the error "POST 500 (Internal Server Error)" is fired

    RépondreSupprimer