PHP code example of cretueusebiu / laravel-javascript

1. Go to this page and download the library: Download cretueusebiu/laravel-javascript library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cretueusebiu / laravel-javascript example snippets


ScriptVariables::add('user', Auth::user());

// config/app.php

'providers' => [
    ...
    Eusebiu\JavaScript\JavaScriptServiceProvider::class,
],

'aliases' => [
    ...
    'ScriptVariables' => Eusebiu\JavaScript\Facades\ScriptVariables::class,
],



namespace App\Http\Controllers;

use Eusebiu\JavaScript\Facades\ScriptVariables;

class HomeController extends Controller
{
    public function home()
    {
        ScriptVariables::add('key', 'value');
        ScriptVariables::add('data.user', User::first());
    }
}

{{ ScriptVariables::render() }}



namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Eusebiu\JavaScript\Facades\ScriptVariables;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        ScriptVariables::add(function () {
            return [
                'csrfToken' => csrf_token(),
                'currentUser' => auth()->user(),
            ];
        });
    }
}