My Blog Posts

Laravel Data Sharing Package

Laravel Data Sharing Package


The API for this package is simple:

Facade
SharedData::put
    'post' => Post::first(),
    'app' => [
        'name' => config('app.name'),
        'environment' => config('app.env'),
    ],
]);

Helper
share([
    'post' => Post::first(),
    'app' => [
        'name' => config('app.name'),
        'environment' => config('app.env'),
    ],
]);

To output data into JavaScript:

window.sharedData = {
    Post: {
        content: "...",
        created_at: "...",
        // ...
    },
    App: { /* ... */ }
}

To print out the configured JavaScript, @shared to your view file Add :

<html>
<head>
  @shared
head>
html>

If you want to configure the JavaScript object, you can publish and modify the configuration:



return ['js_namespace' => 'myCustomObjectName'];

In this package, to help you get started Documents and the source code on GitHub coderello / laravel-shared-data at you can view it .



Share this article


Comments (0)

Comment