Laracasts has a good package that we can use to pass PHP variables to Javascript.

Often, you'll find yourself in situations, where you want to pass some server-side string/array/collection/whatever to your JavaScript. Traditionally, this can be a bit of a pain - especially as your app grows.

This package simplifies the process drastically.

Installation

Begin by installing this package through Composer.

{
    "require": {
        "laracasts/utilities": "~2.0"
    }
}

If you use Laravel 4: instead install ~1.0 of this package (and use the documentation for that release). For Laravel 5 (or non-Laravel), ~2.0 will do the trick! Laravel Users

If you are a Laravel user, there is a service provider you can make use of to automatically prepare the bindings and such.

// config/app.php

'providers' => [
    '...',
    'Laracasts\Utilities\JavaScript\JavaScriptServiceProvider'
];

When this provider is booted, you'll gain access to a helpful JavaScript facade, which you may use in your controllers.

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

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

In Laravel 5, of course add use JavaScript; to the top of your controller.

Using the code above, you'll now be able to access foo, user, and age from your JavaScript.

console.log(foo); // bar
console.log(user); // User Obj
console.log(age); // 29

This pac