0

I wanna input data to database from Select Option Form. I used Laravel and Vue. This is my Form

<div class="form-group">
   <label for="exampleFormControlSelect1">Pemilihan*</label>
   <select class="form-control" id="electionId" name="electionId" v-model="electionIdSelected">
     <option v-for="option in electionIdOptions" v-bind:value="option.value">{{option.text}}</option>
   </select>
</div>

This is my Vue:

data() {
          return{
                 electionIdSelected:'Please choose one',
                 electionIdOptions:[
                   {text: 'Presiden', value: 'ppwp'},
                   {text: 'DPRD Provinsi', value: 'pdpr'}
                 ],
           methods: {
            onSubmit() {
                axios.post('/psu/list/store', {
                   electionIdSelected: this.electionIdSelected,
                }).then(response => {
                   this.electionIdSelected = ''
             });
            }

This is my Controller in Laravel:

public function store(){
      $psu = new Psu;

      $psu->jenis_pemilihan = request('electionId');

      $psu->save();
    }

Please help me to insert form Select option into database. I still got error. This is an Error Message:

null value in column "jenis_pemilihan" violates not-null constraint

Cant get value to Controller and Model.

9
  • what error are you facing. please mention also. Commented Dec 24, 2018 at 8:07
  • 1
    Good question, would be perfect if you could share exactly what the error told you. Commented Dec 24, 2018 at 8:08
  • The js for your vue component seems to be missing some } Commented Dec 24, 2018 at 8:11
  • Like @vahdet said, you need to elaborate on the error, or there's not much to answer. Commented Dec 24, 2018 at 8:15
  • Thank you for respond. I already update Error Message Commented Dec 24, 2018 at 8:20

2 Answers 2

0
public function store(Request $request){
  $psu = new Psu;

  $psu->jenis_pemilihan = $request->input('electionIdSelected');

  $psu->save();
}

I think electionId is empty in your controller because you never set such a key in the axios post ?

Sign up to request clarification or add additional context in comments.

Comments

0

You can try to send electionIdSelected.text for that your selectbox model is electionIdSelected when u set your option.text to the model then your values look like electionIdSelected.text

Just replace this line : electionIdSelected: this.electionIdSelected to electionIdSelected: this.electionIdSelected.text

Comments

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.