Variable assigned within the post in axios travel empty

1

When I send the form data, the controller recognizes them empty.

public function store(Request $request)
    {
        //

        $this->validate($request,[
            'PERS_varDNI' => 'required',
            'PERS_varApPaterno' => 'required',
            'PERS_varApMaterno' => 'required',
            'PERS_varNombres' => 'required',
        ]);

        Persona::create($request->all());


        return;
    }
  

The given data was invalid. {"PERS_varDNI": ["The p e r s var d n i   field is required. "]," PERS_varApPaterno ": [" The p e r s var ap   paterno field is required. "]," PERS_varApMaterno ": [" The p e r s var   ap maternal field is required. "]," PERS_varNombres ": [" The p e r s   var names field is required. "]}

here is where I sent the data

const app = new Vue({
    el: '#v_pers',
    created: function(){
        this.getPersonas();     
    },
    data:{
        personas:[],
        newPersona:{PERS_varDNI: '', PERS_varApPaterno: '', PERS_varApMaterno: '', PERS_varNombres: ''},
        errors:[]
    },
createPersona: function(){
            var url='personas';            
            axios.post(url,{
                persona: this.newPersona
            }).then(response=>{
                this.getPersonas();
                this.newPersona={PERS_varDNI: '', PERS_varApPaterno: '', PERS_varApMaterno: '', PERS_varNombres: ''},
                this.errors=[];
                $('#createPersona').modal('hide');
                toastr.success('Nueva persona creada con exíto');
            }).catch(error=>{
                this.errors=error.response.data
            });
        }

this is the view where I send the data

<form method="POST" v-on:submit.prevent="createPersona">
    <div class="modal fade" id="createPersona">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                    <h4>Nueva persona</h4>
                </div>
                <div class="modal-body">
                    <label for="PERS_varDNI">DNI</label>
                    <input type="number" name="PERS_varDNI" class="form-control" v-model="newPersona.PERS_varDNI">
                    <label for="PERS_varNombres">Nombres</label>
                    <input type="text" name="PERS_varNombres" class="form-control" v-model="newPersona.PERS_varNombres">
                    <label for="PERS_varApPAterno">Apellido Paterno</label>
                    <input type="text" name="PERS_varApPAterno" class="form-control" v-model="newPersona.PERS_varApPaterno">
                    <label for="PERS_varApMaterno">Apellido Materno</label>
                    <input type="text" name="PERS_varApMaterno" class="form-control" v-model="newPersona.PERS_varApMaterno">
                    <span v-for="error in errors" class="text-danger">@{{ error }}</span>
                </div>
                <div class="modal-footer">
                    <input type="submit" class="btn btn-primary" value="Guardar">
                </div>
            </div>
        </div>  
    </div>
</form>

Any idea why the data is not sent? pd: when I use alert (this.newPersona.PERS_varDNI); in the createPersona function there I get the normal dni data

    
asked by hans 23.03.2018 в 15:09
source

1 answer

1

I solved it in 2 ways (the problem is that the request did not recognize

  

person

so it was always sent empty, with the help of @gbianchi add:

  

var person = this.newPersona;

and then just send this and it works

createPersona: function(){
            var url='personas';    
            var persona=this.newPersona;      
            axios.post(url,
                persona
            ).then(response=>{
                this.getPersonas();
                this.newPersona={PERS_varDNI: '', PERS_varApPaterno: '', PERS_varApMaterno: '', PERS_varNombres: ''},
                this.errors=[];
                $('#createPersona').modal('hide');
                toastr.success('Nueva persona creada con exíto');
            }).catch(error=>{
                this.errors=error.response.data
            });
        }

can also be done in front with the variables that are requested in the request thus; When sending the post to axios just add:  axios.post (url, {

  

PERS_varDNI: this.newPersona.PERS_varDNI,                       PERS_varApPaterno: this.newPersona.PERS_varApPaterno,                       PERS_varApMaterno: this.newPersona.PERS_varApMaterno,                       PERS_varName: this.newPersona.PERS_varName,

}) and it worked too.

createPersona: function(){
            var url='personas';  
            alert(this.newPersona.PERS_varDNI);   
            var persona=this.newPersona;
            console.debug(persona);       
            axios.post(url,{
                PERS_varDNI: this.newPersona.PERS_varDNI,
                PERS_varApPaterno: this.newPersona.PERS_varApPaterno,
                PERS_varApMaterno: this.newPersona.PERS_varApMaterno,
                PERS_varNombres: this.newPersona.PERS_varNombres,
            }).then(response=>{
                this.getPersonas();
                this.newPersona={PERS_varDNI: '', PERS_varApPaterno: '', PERS_varApMaterno: '', PERS_varNombres: ''},
                this.errors=[];
                $('#createPersona').modal('hide');
                toastr.success('Nueva persona creada con exíto');
            }).catch(error=>{
                this.errors=error.response.data
            });
        }
    
answered by 23.03.2018 / 15:41
source