简体   繁体   English

Laravel和vue.js值数组

[英]Laravel & vue.js values to array

I'm using vue.js in Laravel. 我在Laravel中使用vue.js。

I've 3 table's. 我有3张桌子。

Article  
Location 
article_location (pivot table)

If I want to convert a location articles to json so I can pass it to vue How could I do this? 如果我想将位置信息转换为json,以便可以将其传递给vue,我该怎么做?

 <div class="container">

        Location->article  //all articles of a location to json

        <template id="ln-data">
            <ul>
                <li v-for="value in list">
                    @{{ value.name }}
                </li>
            </ul>
        </template>

        <script>
        Vue.config.debug = true;

        Vue.component('data', {
            template: '#ln-data',
            props:['list'],

            created() {
                this.list = JSON.parse(this.list);
            }
        })

        new Vue({
            el: 'body'
        });
        </script>

How could I do this? 我该怎么办?

EDIT -- 编辑-

Controller: 控制器:

$location = location::all();
return view('locationproducts')->with('location',$location);

View: 视图:

  <div class="container">
            <data list="{{ $location->article()->toJson() }}"></data>
    </div>

Location model: 位置模型:

public function article()
{
    return $this->belongsToMany('App\article','article_location');
}

Error: 错误:

ErrorException in Macroable.php line 81: Method article does not exist. Macroable.php第81行中的ErrorException:方法文章不存在。 (View: /home/vagrant/Code/project/resources/views/locationproducts.blade.php) (查看:/home/vagrant/Code/project/resources/views/locationproducts.blade.php)

You could use toJson() method, to convert a model to JSON, you may use the toJson method. 您可以使用toJson()方法将模型转换为JSON,也可以使用toJson方法。 Like toArray , the toJson method is recursive, so all attributes and relations will be converted to JSON : toArray一样, toJson方法是递归的,因此所有属性和关系都将转换为JSON:

$location->articles()->toJson();

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM