• 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

PHP's array_map including keys

How to check user Permissions using Custom Middleware in Laravel

How to loop a tree array with unknown depth and get array blocks?

PHP password_verify

PHP 8 - libxml_disable_entity_loader() has been deprecated. How do I replace the functionality it provides?

How do I call Validator from a namespace with an already existing Validator class

Cross-Origin Resource Sharing (CORS) witth Laravel and Vuejs

Composer Curl error 60: SSL certificate problem: unable to get local issuer certificate

How can i hide dt if dd got empty value

401 Unauthorized only occurring on some pages in Laravel 8

How Can I Do LIMIT 1, 2 In WP_Query

Can't remotely connect to MySQL error (13) with PHP script, connecting through CLI works

SMTP ERROR: MAIL FROM command failed: 530 5.7.0 Must issue a STARTTLS command first when using PHPMailer

Limit of log line written to Apache Errorlog from mod php error_log

Passing PHP JSON to Javascript: echo json_encode vs echo json declaration

About Contact Privacy policy Terms and conditions