• 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

How to get median and quartiles/percentiles of an array in JavaScript (or PHP)?

How to send upload image through email

Change user role if checkout custom checkbox is checked in WooCommerce

How do I Post to .txt document before form submits

How can I solve "laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system"?

Laravel Mix Uncaught ReferenceError: $ is not defined

Need to display only array value in JSON output

PHP | "The requested PHP extension bcmath is missing from your system."

How To Access A Column In M-M Relationship Table In Laravel

Laravel 5 session not persisting after user is logged in

laravel MVC execution flow

Laravel implode array items to new lines

How to increase the PHP upload limits [duplicate]

How to KeyBy where multiple items have the same key

Laravel 5 controller sending JSON integer as string

About Contact Privacy policy Terms and conditions