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

soulbork's avatar

How to access PHP variables in JavaScript?

I am creating a game application. Once a User joins a Game they are a Player in that game. When an Admin starts a game, I would like to broadcast that event to all Players.

Following the docs, I can create a private channel in my event with return new PrivateChannel('game.'.$this->game->id);, but I don't know how to pass the game->id to my JavaScript listener gameId variable:

Echo.private(`game.${gameId}`)
    .listen('GameStarted', (e) => {
        console.log('Game started');
    });

I know what the game ID is and my backend scripts all know it, but I don't know how to tell the frontend scripts what it is.

I have considered three ways of doing this:

\1. Place a DB query statement at the front in my routes/web.php file that returns an array of game ID's the user is part of on every page request. e.g:

web.php

<?php 
$games = DB::table('games')
							->select(...)
							->toJson();
echo <script>var games = {{ $games }}</script>

//Creates the dashboard
Route::get('/dashboard', function () {...

This feels like it will create a lot of overhead by performing a DB call on every page request when the information may not be even be needed and I seriously doubt this is the right way to do it, but I'm happy to be told I'm wrong.

\2. Create routes in the routes/api.php file that returns only JSON data and that the JavaScript calls after the page loads. e.g.

dashboard.php

...
</main>	
	<script>
		var games;
		
		if (window.XMLHttpRequest) {
    		games= new XMLHttpRequest();
   			games.open("GET", '/api/games/getUserGames', false);
   		 	games.send(null);
		}
	</script>
</body>
</html>

This feels cleaner and would likely be better and faster from the user experience, but still puts a lot of calls into the DB.

\3. The way that sounds cleanest to me is to find all the users game ID's, player ID's, and any other ID's that might useful when the player logs in and then store those somewhere on the front end. I have no idea how to do this however.

I am using:

  • Laravel 9.42.4
  • Laravel Echo
  • Soketi
  • JQuery

Thanks in advance!

0 likes
4 replies
Sinnbeck's avatar
  1. is a reaaaaly bad idea.
  2. This could work

A simple solution is to just add it to the page in a script

<script>
    window.gameId = {{ $game->id }};
</script>

You can no access it anywhere

vector_rashed's avatar

You can do simply =>

<script>
	var games = "{{ $games }}";
var gameId = "{{ $game->id }}";
</script>
Tray2's avatar

Another would be to store the ids in a cookie or using the local storage, that way you wouldn't need to fetch it if it already exists.

soulbork's avatar

Thanks all for your suggestions. I ended up creating a $userData session array that is filled when the user logs in, then printed between script tags in the page header as suggested. I felt this was the best solution as using a session variable allowed me to persistence without having to make several DB trips and I can maintain control of the required variables in the backend (e.g. if a user joins / leaves a game I can simply add / remove that game ID from the session array).

Please or to participate in this conversation.