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

jeloqim's avatar

Transform PHP Vars to JavaScript with qunit

I am trying to use the Transform PHP Vars to JavaScript and it works in one of my views but not another.

I have a controller and in the controller I reference the JavaScript facade by calling:

use JavaScript;

and in my controller method I have

JavaScript::put(['jsVar' => $phpVar]);

return view('my_view');

and this works. In my_view I can console.log(jsVar) and all is good. Now I have another controller where I do the same thing but whenever I try to access jsVar in a javascript tag I get that it is undefined.

The view is a testing view and I am pulling in qunit javascript files as well.

Anyone have any ideas?

Here is my controller:

<?php

namespace App\Http\Controllers\tests;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use JavaScript;

class TestsController extends Controller
{

    public function comments(){
        $comment = \App\Comment::first();

        JavaScript::put(['commentableId' => 1]);

        return view('comments.comments_tests.comments_tests', compact('comment'));
    }
}

and here is the view:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Comments testing</title>
   
    <link rel="stylesheet" href="/css/qunit.css">
    <script type="text/javascript">
//commentableId comes out undefined
      console.log(commentableId);
    </script>
    <script src="/js/qunit.js"></script>   

  </head>

</html>
0 likes
1 reply
jeloqim's avatar
jeloqim
OP
Best Answer
Level 1

ah, stupidly forgot to include the footer.

Please or to participate in this conversation.