PHPackages                             hihaho/laravel-js-store - 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. [Templating &amp; Views](/categories/templating)
4. /
5. hihaho/laravel-js-store

ActiveLibrary[Templating &amp; Views](/categories/templating)

hihaho/laravel-js-store
=======================

Laravel JS Store - Easily render data to your blade templates to use in your frontend, like Vue

5.1.0(1y ago)455.4k↓28.5%MITPHPPHP ^8.2CI failing

Since Sep 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/hihaho/laravel-js-store)[ Packagist](https://packagist.org/packages/hihaho/laravel-js-store)[ Docs](https://github.com/hihaho/laravel-js-store)[ RSS](/packages/hihaho-laravel-js-store/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (16)Used By (0)

Laravel JS Store
================

[](#laravel-js-store)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d22150fedb1aab31faf56d7ffa6e68c9a1cb7b888c2058cfea6dfcb666d271cc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68696861686f2f6c61726176656c2d6a732d73746f72652e7376673f7374796c653d666c6174)](https://packagist.org/packages/hihaho/laravel-js-store)[![Build Status](https://github.com/hihaho/laravel-js-store/actions/workflows/run-tests.yml/badge.svg?branch=master)](https://github.com/hihaho/hihaho/laravel-js-store)[![Quality Score](https://camo.githubusercontent.com/395418efe77d960fcf18f751dd1c059e071658599fa9dfc0dada436702b89117/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f68696861686f2f6c61726176656c2d6a732d73746f72652e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/hihaho/laravel-js-store)[![Total Downloads](https://camo.githubusercontent.com/744e84482e7de69269fc82b9a14786720d412d57878432e6c70214d030f2c24c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68696861686f2f6c61726176656c2d6a732d73746f72652e7376673f7374796c653d666c6174)](https://packagist.org/packages/hihaho/laravel-js-store)

Easily pass data to your view to create an initial state for your frontend. This package lets you easily create and register global data providers (for each page), which for example is useful for user data. You can also manually push data on the fly, for example in a controller.

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

[](#installation)

You can install the package via composer:

```
composer require hihaho/laravel-js-store:^5.0
```

Next you should render the js data on your page, there are a few different ways to do this:

#### Blade directive

[](#blade-directive)

Add the `@frontend_store` directive to your view:

```

        @frontend_store

```

#### Overwrite the default view

[](#overwrite-the-default-view)

Publish the current one using `php artisan vendor:publish --tag=laravel-js-store-views`or create a new view: `resources/views/vendor/js-store/script.blade.php`

Output the data the way you want and then include it using the blade directive (`@frontend_store`).

Usage
-----

[](#usage)

There are two methods of pushing data to the store, through data-providers or pushing manually.

### Pushing manually

[](#pushing-manually)

At any point in your application you can push data to the store using the helper, facade or through the laravel container.

You can push pretty much any type of data, as long as it can be cast to a string.

```
// Using the helper
frontend_store()->put('user', Auth::user());

// Using the facade
\HiHaHo\LaravelJsStore\StoreFacade::put('user', Auth::user());

// Using the laravel container
app()->make(\HiHaHo\LaravelJsStore\Store::class)->put('user', Auth::user());
app()->make('js-store')->put('user', Auth::user());

// Using the view or response macro
class Controller
{
    public function index()
    {
        return view('index')
            ->js('foo', 'bar');
    }

    public function create()
    {
        return response()
            ->view('create')
            ->js([
                'foo' => 'Fred',
                'bar' => 'Waldo',
            ]);
    }
}
```

### Data-providers

[](#data-providers)

Data-providers are classes that can be used to globally define data that should be send to your frontend. It's also a convenient way to store more logic related to the data, or define a rule if the data needs to be rendered.

The data-providers are defined in your config, so first you'll have to publish the config file.

```
php artisan vendor:publish --tag=laravel-js-store-config
```

This should create `config/js-store.php`.

Create a data provider using the artisan make command:

```
php artisan make:frontend-data-provider SomeName
```

This creates a data-provider which extends `HiHaHo\LaravelJsStore\AbstractFrontendDataProvider`.

An example of a data-provider might look like this:

```
