Question: How to pass value from vue.js to controlller?

Question

How to pass value from vue.js to controlller?

Answers 1
Added at 2017-01-03 23:01
Tags
Question

So i have an array of values in vue.js, but problem is that i need to pass that values to controller. Any suggestion how can i do that, or is that a wrong way?

This is in my vue.js where i get values:

 additional_features: this.additionals_features



this.$http.patch('/profile/'+ user_id +'/property/'+ property_id +'/edit', property_credentials).then(function(response) {

                        $("html, body").animate({ scrollTop: 0 }, "slow");
                        this.property_status = true;
                        this.property_success_update_message = response.data.successUpdateMsg;
                    } , function(response) {
                        $("html, body").animate({ scrollTop: 0 }, "slow");
                        this.property_status = false;
                        this.property_errors = response.data
                    });

This is my controller

  $additional_features = $request->input('additionals_features');

This is my html:

<input type="hidden" v-model="property_credentials.additional_features" name="additional_feature[]" value="@{{property_credentials.additional_features}}" />
Answers
nr: #1 dodano: 2017-01-03 23:01

You can use vue-resource and send it using post:

new Vue({
    el: '#app',

    data: {
        token: '{{ csrf_token() }}',
        property_credentials: {
            additionals_features: [],
        }
    },

    methods: {
        submit: function() {
            this.$http.post('/your-api-url', {
                '_method': 'patch',
                '_token': this.token, // You must pass this token on post requests
                'additionals_features': this.property_credentials.additionals_features,
            });
        }
    }
});

The data will arrive directly in your controller, so you can probably

$additional_features = $request->get('additionals_features');

What you do with this data once it arrives in the controller, it's up to you.

Source Show
◀ Wstecz