You can't do that.
Aug 13, 2016
13
Level 70
Passing VueJS value into PHP Varaible
In one place, I have to use Vuje JS value into PHP variable. My plan is -
<span v-for="message in messages">
<?php
$getUserDetails = App\Message->find( [I need to add *message.id* here ] );
?>
@{{ message.message }}
@{{ message.created_at }}
</span>
Any idea how to do that?
Level 30
If you would like to get the user details from the Database to your Vue Instance you should use vue-resource.
Then you can make a call to your API Endpoint and put this data to your vue data object e.g
data() {
return {
user: {}
}
},
ready() {
this.getTheUser()
},
methods: {
getTheUser() {
this.$http({url: '/user/' + this.$route.params.userId , method: 'GET'}).then(function (response) {
this.$set('user', response.data)
console.log(response.data)
}, function (response) {
console.log(response.data)
});
}
}
This would make a call to your route domain.tld/user/1 for example.
In your Laravel route you can fetch the user e.g
Route::get('/user/{user}', function (App\User $user) {
return $user
});
2 likes
Please or to participate in this conversation.