PHPackages                             kim/javascript - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. kim/javascript

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

kim/javascript
==============

Laravel package to pass PHP variables to your JavaScript files.

1.0.0(10y ago)223MITPHPPHP &gt;=5.4.0

Since Sep 30Pushed 10y ago1 watchersCompare

[ Source](https://github.com/thomastkim/php-to-js)[ Packagist](https://packagist.org/packages/kim/javascript)[ Docs](https://github.com/thomaskim/php-to-javascript)[ RSS](/packages/kim-javascript/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (2)Used By (0)

Use PHP in JavaScript For Laravel 5
===================================

[](#use-php-in-javascript-for-laravel-5)

This package allows you to simply convert and transfer your PHP variables to use in JavaScript.

This is a package I made for practice / fun. Please use [Jeffrey Way's package](https://github.com/laracasts/PHP-Vars-To-Js-Transformer) instead.

Installation
------------

[](#installation)

Installing this package is very easy. Just follow a few quick steps.

### Composer

[](#composer)

Pull this package through composer by opening your composer.json file and adding this under `require`:

```
"kim/javascript": "~1.0"

```

Afterward, run either `composer update` or `composer install`.

### Providers and Aliases

[](#providers-and-aliases)

Open up your `config/app.php` and add this to your providers array:

```
'Kim\JavaScript\JavaScriptServiceProvider'

```

and this to your aliases array:

```
'JavaScript' => 'Kim\JavaScript\JavaScriptFacade'

```

### Config File

[](#config-file)

Finally, publish the package's config file by running this inside your favorite command-line interface:

```
php artisan vendor:publish
```

This command will publish a `javascript.php` file inside your config directory.

Usage
-----

[](#usage)

### Adding a single variable

[](#adding-a-single-variable)

To add or pass a single variable from PHP to JavaScript, you can use simple use the `add` method.

```
JavaScript::add('name', 'Thomas');

```

Now, you can access the `name` variable globally using JavaScript.

```
console.log(name);	// Outputs 'Thomas'

```

Notice that first value is the variable name. The second value is the variable's value. It follows a key-value pairing like an associative array.

### Adding an array of values

[](#adding-an-array-of-values)

You can also pass an array of values.

```
JavaScript::put([
    'foo' => 'bar',
    'pi' => 3.14,
    'isWorking' => true,
    'nothing' => NULL
]);

```

All these will then be accessible globally within your JavaScript files.

```
console.log(foo);		// Outputs 'bar'
console.log(pi);		// Outputs 3.14
console.log(isWorking);	// Outputs true
console.log(nothing);	// Outputs null

```

### Multiple Calls

[](#multiple-calls)

You do not need to call the add or put method in one location. You can call both methods multiple times across multiple files, and it will work just fine.

```
JavaScript::put([
    'foo' => 'bar',
    'pi' => 3.14,
]);
JavaScript::put([
    'isWorking' => true
    'name' => 'Thomas',
]);
JavaScript::add('nothing', NULL);

```

```
console.log(foo);		// Outputs 'bar'
console.log(pi);		// Outputs 3.14
console.log(isWorking);	// Outputs true
console.log(name);		// Outputs 'Thomas'
console.log(nothing);	// Outputs null

```

### Duplicates

[](#duplicates)

The package also makes sure that there are no duplicates so if you add an object in one class and then add it again in another class, it will not conflict. However, if the two values are different, the latest call will overwrite the preceding value.

```
// No conflict
JavaScript::add('name', 'Thomas');
JavaScript::add('name', 'Thomas');

console.log(name); // Outputs 'Thomas'

// New value overwrite old value
$user = User::first();
JavaScript::add('user', $user);
$user->name = "Stephanie";
JavaScript::add('user', $user);

console.log('user'); // Outputs user object with the name of 'Stephanie'

```

### Configuration

[](#configuration)

If you open up your `config/javascript.php` file, you will see two settings. By default, the JavaScript variables are added to the end of your HTML `head`. If you want to add it elsewhere, simply specify the view. It will automatically add the JavaScript variables to the **start** of that view.

You can also specify the namespace and reduce the number of objects / functions that are added to the global scope. If you do change the namespace, remember to reflect that in your JavaScript code. For example, lets say you set the namespace to 'parent'.

```
// Add your data like you normally would.
JavaScript::add('foo', 'bar');

```

```
// Access it from the parent namespace using either brackets or the dot notation.
console.log(parent.foo);	// Outputs 'bar'
console.log(parent['foo']);	// Outputs 'bar'

```

License
-------

[](#license)

This package is free software distributed under the terms of the MIT license.

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3929d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ca22d30322941193b0ebbe83b7594997efcf5ba83da563c32f210e541cd660a3?d=identicon)[thomastkim](/maintainers/thomastkim)

---

Top Contributors

[![thomastkim](https://avatars.githubusercontent.com/u/8099546?v=4)](https://github.com/thomastkim "thomastkim (2 commits)")

---

Tags

phplaraveljavascriptphp to javascript

### Embed Badge

![Health badge](/badges/kim-javascript/health.svg)

```
[![Health](https://phpackages.com/badges/kim-javascript/health.svg)](https://phpackages.com/packages/kim-javascript)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[bezhansalleh/filament-google-analytics

Google Analytics integration for FilamentPHP

211189.7k8](/packages/bezhansalleh-filament-google-analytics)[emargareten/inertia-modal

Inertia Modal is a Laravel package that lets you implement backend-driven modal dialogs for Inertia apps.

90142.9k](/packages/emargareten-inertia-modal)[wearepixel/laravel-cart

A cart implementation for Laravel

1374.8k](/packages/wearepixel-laravel-cart)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.4k](/packages/tomshaw-electricgrid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
