laracasts / PHP-Vars-To-Js-Transformer

Transform PHP data to JavaScript.
https://packagist.org/packages/laracasts/utilities
MIT License
2.21k stars 219 forks source link

Not Working for Laravel 5.4 - Error foo is not defined #106

Open svs22121990 opened 7 years ago

svs22121990 commented 7 years ago

I have installed the "laracasts/utilities": "3.0" package.

Add Laracasts\Utilities\JavaScript\JavaScriptServiceProvider::class to the providers array

I got Uncaught ReferenceError: foo is not defined in the console.

Then, I also tried changing the javascript config file and changed 'bind_js_vars_to_this_view' => 'foot' where, foot.blade.php is a file in the subfolder in resources. Do I need to specify the entire file path ? I tried that but, that also, didn't work for me.

P4sca1 commented 7 years ago

Same for me with Laravel 5.5

binmosa commented 7 years ago

I am getting same issue for Laravel 5.4 . Uncaught ReferenceError: foo is not defined I've used both versions 2.0 and 3.0 , but getting same error. It seems the binding did not work for some reason. Any solution?

binmosa commented 7 years ago

UPDATE : I've soled my problem and now my code is working on Laravel 5.4. For those who have the same issue , please try the following steps (as I did) it may help to solve your problem:

1. before installation, make sure that the package's requirements are there :

--- requires : php: >=5.4.0 illuminate/support: ~5.0

--- requires (dev) : phpspec/phpspec: ~2.0 (if this one is missing in your composer file, add it and update the composer).

2. install the package : composer require laracasts/utilities

3. Add the service provider in config\app.php : Laracasts\Utilities\JavaScript\JavaScriptServiceProvider::class, 4. create an empty footer file in the root of view folder. footer.blade.php 5. include the empty footer in the view you want the javascript vars to be available in. @include('footer') 6. Go to the controller of the view in step (5) and add : use JavaScript; at the top.

7. in the same controller, now you can declare the array of your variables inside one of the methods as :


public function index()
{
    JavaScript::put([
        'foo' => 'bar',
        'user' => User::first(),
        'age' => 29
    ]);

    return View::make('hello');
}

8. now go to the view and write something like :

<script>
    console.log(foo);
    console.log(age);
</script>

and check the console. I hope it works.

P4sca1 commented 7 years ago

Thanks for your answer @binmosa. I'll follow these steps and get back to this issue later.

P4sca1 commented 7 years ago

I only needed to install the package via composer require and everything started to work. (I don't needed to add the service provider because of laravel 5.5).

The problem before was, that I was overwriting the js_namespace in custom javascript. My bad :3

danigunawan commented 6 years ago

thanks for information

AngelGris commented 6 years ago

It isn't working for me in Laravel 5.5. Changed the spaceName to reactBind and when I do console.log(window) I see the reactBind object with all the values I created, but console.log(window.reactBind) is undefined and console.log(reactBind) throws a ReferenceError. Any extra step required to use this with React?

AngelGris commented 6 years ago

Solved the problem. I was including the view that prepends the code after the app.js inclusion, and it should be included before that.