Summer Sale! All accounts are 50% off this week.

Lars-Janssen's avatar

Vue.js object to json

Hello,

How could I get this to work?

 <div class="container">
        <data list="{{ $location->article()->toJson() }}"></data>
        <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>

Controller:

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

Location model:

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

Error message:

ErrorException in Macroable.php line 81: Method article does not exist. (View: /home/vagrant/Code/project/resources/views/locationproducts.blade.php)

Thankyou.

0 likes
4 replies
Francismori7's avatar

In fact, cross that. You are using Location:all() which returns a collection, namely a list of locations. To get the articles you need a loop that goes through the collection and then on each of the items you can call the Location methods.

Lars-Janssen's avatar

@Francismori7

Thanks for your reply.

I've 3 tables:

Article  
id
name
description

Location 
id
place

article_location (pivot table)
--fields--
id
article_id (foreign key)
location_id (foreign key)

How could I get the following fields with eloquent:

article.place
article.description
location.place

(And then convert it to json)

That would solve my problem.

Thanks again!

Please or to participate in this conversation.