2

I have a form used to post product in a shopping cart, I have a Vuejs component setup and it retrieves data just fine, but when I try adding a select tag, I can't pass the data from it. Here's my code for the blade.php file:

    <form action="{{ url('/cart') }}" method="POST" class="side-by-side">
  <input type="hidden" name="id" v-model="this.id" value="{{ $product->id }}">
  <input type="hidden" name="name" v-model="this.name" value="{{ $product->name }}">
  <input type="hidden" name="price" v-model="this.price" value="{{ $product->price }}">

{{-- Can't figure out how to pass the following --}}

<select  v-model="selected"  required>

            <option disabled value="">Please select one</option>
            @foreach($product->withOptions() as $option)
            <option value="{{ $option->options }}">{{ $option->options }}</option>
            @endforeach

 </select>
 <addToCart :product="{{ $product }}"></addToCart>

and here's my vue file:

export default {
    props: ['product'],

        data() {
            return {
                id: this.product.id,
                quantity: 1,
                name: this.product.name,
                price: this.product.price,
                selected: ''  // always null, can't figure it out
        }
    },

    methods: {
        addtocart() {
            axios.post('/cart/', this.$data)
            .then(flash(this.product.name + ' was added to cart'));
        }
    }
}

Thank you for your help.

3 Answers 3

1

Solved that way:

<addToCart :product="{{ $product }}" :selected="selected"></addToCart>

And in the vue file :

 export default {
    props: ['product', 'selected'],

        data() {
            return {
                id: this.product.id,
                quantity: 1,
                name: this.product.name,
                price: this.product.price,
                options: this.selected
        }
    },

    watch: {
        selected: function() {
            return this.options = this.selected
        }
    },

    methods: {
        addtocart() {
            axios.post('/cart/', this.$data)
            .then(flash(this.product.name + ' was added to cart'));
        },

Just added a watcher thing.

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

Comments

1

try this:

<select  v-model="selected"  required>
    <option disabled value="">Please select one</option>
   <option v-for="option in product" v-bind:value="product.option">{{product.option}}</option>
</select>

Comments

0

In your blade.php file

<addToCart :product="{{ $product }}" :selected="selected" ></addToCart>

Now in your Vue component, you can access the value of selected like this

 export default {
    props: ['product','selected'],

        data() {
            return {
                id: this.product.id,
                quantity: 1,
                name: this.product.name,
                price: this.product.price,
                selected: this.selected 
           }
        },
        methods: {
          addtocart() {
            self= this;
            axios.post('/cart/',{
                id : self.id,
                quantity: self.quantity,
                name: self.name,
                price: self.price,
                selected: self.selected
             })
            .then(function (response) {
               console.log(response.data);
            })
           .catch(function (error) {
              console.log(error);
            })  
          }
        }
   }

8 Comments

Thank you for your answer, this lets me select it, but it still doesn't gets sent with the post request tho, the option just stays null..
@Laurent since you are getting selected in your vue component I updated the methods sections. Please try again. Note that you cannot use this directed inside axios which is already inside a method. So i refer this with self.
I tried it again, turned it in many many different ways, no luck yet.. Thanks for your help
@Laurent please confirm that you are getting selected prop in your component and your current problem is with posting datas to server
@Laurent i edited the answer and please try again. And if axios post not working give the console log error report
|

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.