• 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

Yii2 redirecting to previous page after login

Laravel Multi-language routes without prefix

Laravel relationship belongsToMany with composite primary keys

how to fix Service provider class not found when using repository?

How to update array value in Laravel

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'posts.post' doesn't exist

Access relation of pivot table in Laravel

Image upload not working through ajax Laravel

How to map the two arrays with a duplicate value?

Select option from dropdown menu with PHP and mySql

How to auto populate preferredCountries from intl-tel-input with db output

how to retrieve the first and last instance of a row in pdo dataset

Toggle between a textbox and a select using PHP

Laravel 5 issue with wherePivot

How to average columns of data from multiple, flat arrays?

About Contact Privacy policy Terms and conditions