jeudi 10 mai 2018

Retrieve 'username' from Articles table

I have two tables, 'users' and 'articles'. Articles have a column 'user_id' which is a foreign key that references the user_id in 'users'.

I have in the Articles model this function which should return the user data:

public function user()
{
    return $this->belongsTo('App\User');
}

And this works fine when I pass my articles to my viewer and call it in blade template:

@foreach($articles as $article)
    <p></p>
@endforeach

But I am trying to use the RESTful approach, so I am rather retrieving my data from JS (VueJS)

axios.get('/api/articles')

that should fire my Controller's function:

public function index()
{
    $books =  bookpost::all();
    return $books;
}

So I was wondering if there's a way to append the user names to the JSON array of articles before returning it because in JS I couldn't get to find a way to get the username.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2jNIob5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire