Laravel And AJAX

Posted 4 years ago by MarkMatute

I cant save my data, is returning error 500, AJAX Code

public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            "firstname"=>"required",
            "lastname"=>"required",
            "username"=>"required|unique:users",
            "password"=>"required|min:8|confirmed"
        ]);

        if($validator->fails()){
            return response(['valid'=>false,'errors'=>$validator->errors()]);
        }

        if(Employee::create($request->all())){
            return response::json(array(
                'valid'=>true
            ));
        }
    }

My Request

    $("#saveEmployeeForm").submit(function(e){
        e.preventDefault();
        $("#saveEmployeeForm .form-group").removeClass("has-error");
        var formData = $(this).serialize();

        $.ajax({
            beforSend:function(){

            },
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            url:"store",
            type:"POST",
            data:formData,
            success: function(data){
                if(!data.valid){
                    var keys = Object.keys(data.errors);
                    $.each(keys,function(index,value){
                        setErrorToField("#"+value);
                    });
                }else{
                    console.log(data);
                }
            },
            complete: function(){

            },
            error: function(err){
                console.log(err);
            }
        });
    });

Please sign in or create an account to participate in this conversation.