Package for sharing data from Laravel to JavaScript.
coderello/laravel-shared-data is a Laravel package for package for sharing data from laravel to javascript..
It currently has 313 GitHub stars and 77.667 downloads on Packagist (latest version 4.0.0).
Install it with composer require coderello/laravel-shared-data.
Discover more Laravel packages by coderello
or browse all Laravel packages to compare alternatives.
Last updated

Laravel Shared Data provides an easy way to share the data from your backend to the JavaScript.
You can install this package via composer using this command:
composer require coderello/laravel-shared-data
The package will automatically register itself.
First of all, you need to include this line before all of your <script> tags in you base blade layout in order to make the shared data available in all of those scripts.
{!! shared()->render() !!}
Now you can share any data you want from any part or your application (middleware, controller, service provider etc.)
use Coderello\SharedData\Facades\SharedData;
public function index()
{
SharedData::put([
'user' => auth()->user(),
'post' => Post::first(),
'username' => '@hivokas',
]);
// or
share([
'user' => auth()->user(),
'post' => Post::first(),
'username' => '@hivokas',
]);
}
And get this data on the frontend side from window.sharedData (use can modify the namespace in the config file).

You can run the tests with:
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.