Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AydenWH's avatar

v-for - Unable to Display Result in <option> Tag

I am having difficulty to display the result on drop-down list because the v-for is not working in <option> tag.

View

<select name="server" v-model="newWebsiteItems.server">
    <option selected>Choose your server</option>
    <option v-for="server in servers" value="@{{ server.id }}">@{{ server.server_name }}</option>
</select>

Vue

new Vue({
    el: '#website-listing',
    data: {
        newWebsiteItems: {
            website : '',
            duration : 0,
            purchased_date : '',
            server: ''
        },
    },
    ready: function() {
        this.getWebsiteItems();
    },
    methods: {
        getWebsiteItems: function() {
            NProgress.start();
            this.$http.get('/websites').then((response) => {
                this.$set('servers', response.data.servers);
                this.$set('websites', response.data.websites);
            });
            NProgress.done();
            NProgress.remove();
        },
        createWebsite: function() {
           ...
        },
    }
})

PS: I tried to use Laravel foreach to populate the result and use v-model to fetch the data. But Vue fetch the name instead of the value from the option tag.

0 likes
27 replies
jaydeluca's avatar

Try putting the v-for in a template tag instead.

<template v-for="server in servers">
    <option value="@{{ server.id }}">
        @{{ server.server_name }}
    </option>
</template>
InaniELHoussain's avatar

I've used this one and it worked, make sure its not empty

in div with ID= app

 <select>
  <option v-for="item in items" value="{{ item.code }}">{{ item.name }}</option>
</select> 

JS code

new Vue({
el: '#app',
data: {
    items: [ 
        { name : 'stylo', code : '001', visible : false, hiddenText : 'Iam product One'},
        { name : 'papier', code : '003', visible : true, hiddenText : 'Iam product two'},
        { name : 'cleaner', code : '004', visible : true, hiddenText : 'Iam product three'},
    ]
})
tykus's avatar

@Tyris your <option...> code looks ok, so start by having a look at the data you are actually receiving from the API; it is possible that the structure is slightly different from what you are expecting. Vue will dump errors and warnings into the console if you use the unminified version and set the debug level.

Vue.config.debug = true
1 like
AydenWH's avatar

@tykus_ikus

I tried to implement Vue.config.debug = true and it seems my servers variable is fine.

I can view the information by using console.log but when it is unable to v-for server list.

InaniELHoussain's avatar

@Tyris are you calling the getWebsiteItems method from the ready function to load the data that will be shown

new Vue({

el: '#app',
ready: function(){
    this.getWebsiteItems()
},
methods: {
    getWebsiteItems: function() {
        http.get('/websites').then((response) => {
            this.$set('servers', response.data.server);
        });
    }       

}
})
3 likes
AydenWH's avatar

@InaniELHoussain, yes. What you are doing is almost exactly the same as what I had done.

new Vue({
    el: '#website-listing',
    data: {
        ...
    },
    ready: function() {
        this.getWebsiteItems();
    },
    methods: {
        getWebsiteItems: function() {
            this.$http.get('/websites').then((response) => {
                this.$set('servers', response.data.servers);
                this.$set('websites', response.data.websites);
            });
        }
    }
});
AydenWH's avatar

@alenabdula ,

This is the result when I console.log(response.data.servers)`

[Object, Object]
0:Object
1:Object
InaniELHoussain's avatar

@Tyris try this way it should work

new Vue({

el: '#app',
ready: function(){
    this.getWebsiteItems()
},
methods: {
    getWebsiteItems: function() {
        http.get('/websites').then((response) => {
            this.$set('servers', response.data);
     });
 }       

}
})
AydenWH's avatar

@InaniELHoussain ,

Still no luck to list out all the server.

It shows empty option.

I guess I have to use Laravel foreach loop to display it.

JoolsMcFly's avatar

Any way you could post what GET /websites returns (what you get in Network panel)?

And how about having a default value for servers in data:

data: {
  servers: []
}

and call this.servers = response.data.servers, does that change anything?

Last question, is your directly in #app or in a component?

AydenWH's avatar

@JoolsMcFly ,

Hi JoolsMcFly,

Sorry I could not give you any data that come from Network Panel.

I never assign any default value for server in data. I just leave it blank. *Of course, I tried to assign the value into server in data but the v-for is not working either.

The method you shows this.servers = response.data.servers I had also tried. It still the same. no luck.

But it manages to display the correct value when I am using @{{ servers[0].server_name }}

Last question, is your directly in #app or in a component?

It is inside #app.

I have really no idea why this is not working.

Everything seems fine, is that mean I have to maually key in the value into the server by myself?

data" {
    server: [
        { text: 'One', value: 'A' },
        { text: 'Two', value: 'B' },
        { text: 'Three', value: 'C' }
    ]
}
AydenWH's avatar

@InaniELHoussain,

I updated my code already.

Please let me know if you are looking for other codes also.

Thank you very much. Much appreciated for your help.

I can share my source file to you if you want to.

joedawson's avatar

What's this?

data" {
    server: [
        { text: 'One', value: 'A' },
        { text: 'Two', value: 'B' },
        { text: 'Three', value: 'C' }
    ]
}

The syntax is wrong. It should be this.

data: function() {
    return {
        servers: [
            { text: 'One', value: 'A' },
            { text: 'Two', value: 'B' },
            { text: 'Three', value: 'C' }
        ]
    }
}

Your data key was also "server" and not "servers"

AydenWH's avatar

@JoeDawson ,

It's just an example. Don't too serious about it.

My actual coding was posted at the first post.

joedawson's avatar

@tyris then how do you expect people to help you debug lol? paste your code :D

tjames's avatar

It's not working for multiple reasons:

  1. You are trying to loop over a property (servers) in your data object that you haven't initialized which means no watcher is created for that value so even if you add it later the loop won't work. It should look like the code below:
data: {
        newWebsiteItems: {
            website : '',
            duration : 0,
            purchased_date : '',
            server: ''
        },
    servers: []
},
  1. Your api call doesn't actually set the servers in the data object:
this.$http.get('/websites').then((response) => {
                this.$set('options', response.data.servers); // <-- you are setting an 'options' property with the server data, not 'servers'
                this.$set('websites', response.data.websites);
            });
AydenWH's avatar

@tjames

I tried to defined servers inside data previously, it still not working.

I also replace the options to servers

<option> won't loop but if you use servers[0].server_name then it will come out with the correct data.

joedawson's avatar

Firstly, your template is correct (v-for etc). Here's a working example of using v-for in a select.

https://jsfiddle.net/mc6a8L4L/

So the issue isn't with Vue here, not at least to my understanding of everything so far.

I believe the issue is related to how your fetching the servers. How're you currently returning the servers and websites on your /websites endpoint?

Vue-Resource will return any response in a data object. But Laravel also returns it's responses in a data object.

Where you currently have this:

this.$set('servers', response.data.servers);
this.$set('websites', response.data.websites);

Have you tried this?

this.$set('servers', response.data.servers.data);
this.$set('websites', response.data.websites.data);

Please or to participate in this conversation.