• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

How in Laravel run JavaScript code stored in php variable?

phplaravellaravel-livewire


PHP Snippet 1:

public $foo;

$this->dispatchBrowserEvent('custom-event', ['foo' => $this->foo]);

PHP Snippet 2:

<script>
    window.addEventListener('custom-event', event => {
          alert('here value foo: ' + event.detail.foo);
          console.log(event.detail.foo);
    })
</script>

PHP Snippet 3:

<script>
    document.addEventListener('custom-event', () => {
        {!! json_encode($script) !!}
    })
</script>

Related Snippets

Use same method for inertia response and json response Laravel Jetstream

laravel sanctum Route [login] not defined

laravel and phpunit: could not find driver (SQL: PRAGMA foreign_keys = ON;)

retrieve data from database using session

args[max_input] woocommerce if statement confused

oauth-private.key does not exist or is not readable

(Laravel) How to delete multiple models including optional relationships?

Laravel Model save() & update() Not Saving

Parameter is not sent to Laravel route in Ajax

Phpunit partial mock + proxy Entity

How do I refresh a DIV content?

Error Class "LaravelFortifyFeatures" not found

how to prevent float variables displaying as scientific notation when printing [duplicate]

jwt decode throws Invalid argument supplied for foreach() on php-jwt and codeignitor 4

Unable to guess the mime type as no guessers are available Laravel 5.2

About Contact Privacy policy Terms and conditions