Giter Site home page Giter Site logo

laravel-javascript's Introduction

laravel-javascript

Latest Stable Version Build Status Total Downloads

Add JavaScript variables from Laravel.

ScriptVariables::add('user', Auth::user());
const user = window.config.user

Installation

Install the package via Composer:

composer require cretueusebiu/laravel-javascript

Next, you need to register the service provider and facade:

// config/app.php

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

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

Usage

In your controller:

<?php

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());
    }
}

Next, in your blade view add:

{{ ScriptVariables::render() }}

Then in your JavaScript you can use:

const key = window.config.key
const user = window.config.data.user

To customize the namespace use ScriptVariables::render('custom').

Global Variables

You can register global variables (like the current user or csrf token) in your AppServiceProvider:

<?php

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(),
            ];
        });
    }
}

Note that the variables must be passed via a closure.

laravel-javascript's People

Contributors

cretueusebiu avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

lapiy dbbold zeevx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.