Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tisuchi's avatar
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?

0 likes
13 replies
tisuchi's avatar
Level 70

well... then how to get a specific users details from database ?

1 like
tykus's avatar

@tisuchi you appear have a users collection/array already in Vue - why would you want to fetch each user again individually from the database?

mahmandev's avatar

You can't pass client side variables into the server side. It doesn't work like that.

tisuchi's avatar
Level 70

@tykus_ikus

I think I wrote wrongly. Already updated the question... plz check again...

1 like
Snapey's avatar

You cannot run PHP from the client. If you need to dynamically get some data you need to place a request on the server with ajax

Time for a rethink

2 likes
tisuchi's avatar
Level 70

ok, thinking in different way....

what if I just call an another method in vue that can bring users details and from template, I will call this method?

If it is, there are 2 cases-

  • I will provide an Id in a method as a parameter to fetch all the details of that particular user. From where (globally) I need to call that method?
  • How to call a method in template area ?
1 like
tykus's avatar

I don't understand why don't you just use the users that you already have available in the Vue data?

topvillas's avatar

You can't do this. You'll need to use VueResource or some other AJAX client to get the message from a sever endpoint.

tykus's avatar
<span v-for="message in messages">

Sorry it's messages now

christopher's avatar
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
jekinney's avatar

You can pass php data as a prop into a component or use data attributes.

Please or to participate in this conversation.