Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

337
Visualizações
Passing php variables to javascript in laravel syntax errors

Question 1) When passing PHP variables to be used in javascript if the variables hold an array, it will result in an error relating to the attempted conversion of array to string when trying to pass these values in the following manner. Why do I have to json encode the value before it works when $var itself is already an array?

PHP Code

$var = ['test'];

Javascript

Let var = {!!$var!!};

Solution

Let var = {!!json_encode($var)!!} 

Question 2 when passing PHP variables that may have the value of null to javascript, it may result in a syntax error. For example,

PHP Code

$var = null;

Javascript

Let var = {{$var}};

Error

Var = ; (Syntax error)

Failed Solution

if({{$var}}){
    let var = {{$var}};

Solution

Let var = '{{$var');

Why does my attempted solution of only setting the value of the var when $var has a value fails?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

simply put laravel echo in doouble quote

<script>
 let var1="{{$var}}";
</script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda