• 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

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

Image upload not working through ajax Laravel

target [LaravelFortifyContractsRegisterViewResponse] is not instantiable

How to show selected value using javascript in laravel

array_key_exists(): The first argument should be either a string or an integer

I want a way to give path to my files which are outside of public folder in laravel

Check If array is null or not in php

Display the WooCommerce product price inside a custom function

Cannot pass null argument when using type hinting

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given [duplicate]

Laravel 8 Multiple Relationships for Factory

PHP: Convert any string to UTF-8 without knowing the original character set, or at least try

Failing validation doesn't stop code execution in livewire component

dockerizing Laravel + vue

How to remove Deprecated SymfonyComponentDebugDebugClassLoader?

About Contact Privacy policy Terms and conditions