Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

333
Views
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 answers
Answer question

0

simply put laravel echo in doouble quote

<script>
 let var1="{{$var}}";
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!