search

Home  >  Q&A  >  body text

vue.js - The data returned by laravel's joint query is displayed using vuejs. The original data table can be displayed, but the associated table data cannot be displayed.

Involves 3 data tables: users, articles, images
Users and articles, one-to-many, that is, one user can have multiple articles
articles and images, one-to-one, that is, one article The article has a picture, (although the picture can be used as a column in the articles table, we will not do so here)

Controller: Query the articles of the currently logged in user and the pictures corresponding to the articles, return them, and use vuejs to receive them at the front desk.

    public function articles()
    {
        $user=\Auth::user();
        $articles = $user->articles()->with('images')->get();
        return $articles;
    }

javascript:

    <script type="text/javascript">
        Vue.config.debug = true;
        var vm = new Vue({
            el: "#app",
            data: function(){
                return{
                    items: []
                }
            },
            created() {
                var article=this;
               /* $.getJSON('/articles/image',function(data){
                    console.log(data);
                    article.items=data;
                });*/

                this.$http.get('/articles/image').then((response) => {
                    console.log(response.body);
                    article.items=response.body;
                }, (response) => {
                    // error callback
                });
            }
        });
    </script>

html:

                <p class="row" v-for="(index, item) in items">
                    
                    <p class="col-xs-2">
                        @{{ item.title }}
                    </p>
                    
                    <p class="col-xs-2">
                        @{{ item.image }}
                    </p>
                </p>

There are two problems. The code is in the js part:
1. Use $.getJSON to send an ajax request and return an object. The title field in the html can be displayed normally. The image field of the related table cannot be displayed. How should I write it to display it?

2. When using vue-resource code to send an ajax request, an array is returned, and the elements in the array are objects. At this time, the title field cannot be displayed, and of course the image field will not be displayed. How should it be written? show?

仅有的幸福仅有的幸福2743 days ago649

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 16:53:15

    1. item.image.image

    2. Use response.data或者response.json

    3. instead

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 16:53:15

    response.data

    reply
    0
  • Cancelreply