Reputation: 267
How do I send data from Laravel Controller to vue? I have it set up how I thought it might work but no data is being sent. What have I done wrong? Controller(set in a separate API folder):
public function index()
{
return User::all();
}
Route from api.php:
Route::apiResources([
'user' => 'API\UsersController'
]);
Component code:
<template>
<div>
<v-toolbar flat color="white">
<v-toolbar-title>Users</v-toolbar-title>
</v-toolbar>
<ol v-for="user in users">
<li>Name: {{ user.name }}</li>
</ol>
<v-data-table
:headers="headers"
:items="users"
:items-per-page="5"
class="elevation-1"
flat
>
</v-data-table>
</div>
</template>
<script>
import {AxiosInstance as axios} from "axios";
export default {
data () {
return {
headers: [
{
text: 'Username',
align: 'left',
value: 'username',
},
{ text: 'Name', value: 'name' },
{ text: 'Surname', value: 'surname' },
{ text: 'Email', value: 'email' },
],
users: [],
}
},
methods: {
loadUsers(){
axios.get('./api/user').then(response => this.users = response.data);
}
},
mounted() {
this.loadUsers();
}
}
</script>
I just put a simple list up there to test the array but it just comes out blank.
Upvotes: 0
Views: 1922
Reputation: 2355
replace this
axios.get('./api/user').then(response => this.users = response.data);
with this
axios.get("api/user").then(({data}) => (this.users = data));
Upvotes: 2