• 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

Laravel UUID generation

Laravel 5.2: Unable to locate factory with name [default]

Star and Half Star Rating in Laravel

Show date difference as "20" instead of "20 years ago"

storagelogs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied

PHP Discord OAUTH2 code sample not working

How to create a scheduler application in php

Set quantity minimum, maximum and step at product level in Woocommerce

Form repeater send wrong data with last element in Laravel

How to get the ID of the link in another page in php

laravel 5.6 bulk inserting json data

Laravel Pagination links() not working

How do I truncate a decimal in PHP?

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

How to update array value in Laravel

About Contact Privacy policy Terms and conditions