1

I want to make a record after validation of data sent with ajax. the idea is to run the function CreateNewUser if no errors, but does not store the data in the DB. that will happen this?

Thank you.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Modals\NewUser;

class UserController extends Controller
{
    public function index()
    {
        return view('home');
    }
    public function NewUser(Request $request)
    {
        $validator= Validator::make($request->all(),[

            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:8|same:cpassword',
            'cpassword'=> 'required',

        ]);

        if($validator->fails()){
            return response()->json([
                'success' => false,
                'error' => $validator->errors()->toArray()
            ]);


        }else{
            return response()->json(['success' => true ]);

            $this->CreateNewUser($request);
        }

    }

    private function CreateNewUser(Request $requestt){

        $user = new NewUser;
        $user->name = $requestt->name;
        $user->email = $requestt->email;
        $user->passwosrd = $requestt->password;
        $user->rememberToken = $requestt->_token;
        $user->save();
    }


}

model:

<?php

namespace App\Modals;

use Illuminate\Database\Eloquent\Model;

class NewUser extends Model
{
    protected $table = 'users';
}

js:

function send(event){

    event.preventDefault();

    $.ajax({


        type: 'post',
        url: userAjax,
        dataType: 'json',
        data: {
            name: $('#name').val(),
            email: $('#correo').val(),
            password: $('#password').val(),
            cpassword: $('#cpassword').val(),
            _token: $('#new-user').attr('data-token')
        },
        beforeSend: function()
        {
            $("#nameError").fadeOut();
            $("#emailError").fadeOut();
            $("#passwordError").fadeOut();
            $("#cpasswordError").fadeOut();

        },

        success: function(data){



           if(!data.success){
               console.log(data);
               if(typeof data.error.name !== 'undefined'){

                   $('#name').css('border-color', 'red');
                   $('#nameError').fadeIn(10, function () {
                       $("#nameError").html(data.error.name);

                   })
               }
               if(typeof data.error.email !== 'undefined'){
                   $('#correo').css('border-color', 'red');
                   $('#emailError').fadeIn(10, function () {
                       $("#emailError").html(data.error.email);

                   })
               }
               if(typeof data.error.password !== 'undefined'){
                   $('#password').css('border-color', 'red');
                   $('#passwordError').fadeIn(10, function () {
                       $("#passwordError").html(data.error.password[0]);


                   })
               }
               if(typeof data.error.cpassword !== 'undefined'){
                   $('#cpassword').css('border-color', 'red');
                   $('#cpasswordError').fadeIn(10, function () {
                       $("#cpasswordError").html(data.error.cpassword);


                   })
               }


            }else{
               console.log(data);
               $('#Register').modal('hide');
               $("#header").replaceWith($('#header'));
            }

        },
        error: function() {}

    });

}

error:

console.log(data):

jquery.min.js:4 
POST http://localhost:8000/NewUser 500 (Internal Server Error)
send @ jquery.min.js:4
ajax @ jquery.min.js:4
send @ scripts.js:5on
click @ (index):158

1 Answer 1

1

The Function CreateNewUser is never called because you are returning before the function call which breaks the scope and $this->CreateNewUser($request); is never called.

public function NewUser(Request $request)
    {
        $validator= Validator::make($request->all(),[

            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:8|same:cpassword',
            'cpassword'=> 'required',

        ]);

        if($validator->fails()){
            return response()->json([
                'success' => false,
                'error' => $validator->errors()->toArray()
            ]);


        }else{
            return response()->json(['success' => true ]);

            $this->CreateNewUser($request);
        }

    }

To make it work change the else code to

else {
      $this->CreateNewUser($request);
  }
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, I changed the code and keeps coming out the same.
are you getting this error now POST http://localhost:8000/NewUser 500 (Internal Server Error)
More or less, I had this problem, and too the name of the token remember in the database
I am glad to help you !

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.