Patrick Stek
Patrick Stek

Reputation: 77

Controller fuction How to switch the data from false to true

I'm learning laravel with vuejs. So i have a question. When the tfaEnabled button is pressed then must the database set to true. This colum is by default false.

and when i clicked on the button he needs go to true. I hope one of you can help me out of this.

ps (Sorry for my bad english)

DatabaseColum: this is de colum of users 2fa

This is my 2fa.vue:

<template>
    <v-container class="user-form-lime" fluid grid-list-xl>
        <v-form>

            <v-layout row wrap>

                <v-flex xs12 md6 xl3>

                     <v-switch v-model="tfaEnabled"
                                   label="Tweefactor authenticatie"
                                   name="tfaEnabled"
                                   prepend-icon="lock"
                                   @change="change" />


                    <v-flex xs12>


                        <v-text-field v-if="tfaEnabled"
                                      v-model="google2fa.token"
                                      label="Token"
                                      :rules="[rules.required]"
                                      type="text" />
                    </v-flex>

                </v-flex>

            </v-layout>

            <v-layout row wrap>

                <v-flex xs12>
                    <v-btn color="success" @click="submit">
                        Opslaan
                    </v-btn>
                </v-flex>

            </v-layout>

        </v-form>

    </v-container>

</template>

<script>
    export default {
    name: 'UserForm2fa',
    props: {
        id: { type: Number, required: true }
    },
    data() {
        return {
            tfaEnabled: false,
            google2fa: {
                token: '',
            },
            rules: {
                    required: val => !!val || 'Dit veld mag niet leeg zijn',
            }
        };
    },
    methods: {
        changeStatus() {
            this.$emit( 'change', this.tfaEnabled );
        },
        submit() {
            this.$emit('submit', this.token)
        }
    }
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

This is my controller:

public function update2faStatus( Update2faSatus $request ) {

        $user = User::findOrFail( $request->id );

       if($request->input('tfaEnabled'){

        

       });
        
    }

Upvotes: 0

Views: 70

Answers (1)

Alex
Alex

Reputation: 433

I would do something like this:

public function update2faStatus(Update2faSatus $request) {

    $user = User::findOrFail($request->id);

    $tfaEnabled = $request->input('tfaEnabled', false);
    $user->tfaEnabled = $tfaEnabled;

    $user->save();
}

So if the requst contains tfaEnabled it will take that value and store it into $tfaEnabled, otherwise defaults to false (when the checkbox is not checked, for instance).

Upvotes: 1

Related Questions