Populating js array in blade template
Asked Answered
R

3

6

I'm trying to build a javascript array of data for use in flot from the result of a php DB query.

When I'm outputting numbers ($lic->Users) it's fine, but whenever I try to output a string I get a completely blank page in the browser.

I've tried:

  • {{ $lic->CompanyName }}
  • "<?php echo $lic->CompanyName; ?>"
  • {{{ $lic->CompanyName }}}
  • '"'+ <?php echo $lic->CompanyName; ?> +'"'

But when I hard-code it (to say, 'CompanyName'), it builds the bar graph (& displays the page) just fine.

var data = 
[
    @foreach($licdata as $lic)
    {
    'label': "{{ $lic->CompanyName }}",
    'data': [
                ["{{ $lic->CompanyName }}", {{ $lic->Users }}], 
                ["{{ $lic->CompanyName }}", {{ $lic->Emps }}]
            ]
    },
    @endforeach
];

I think it must be something to do with quoting/escaping the string in js, but I can't work out what, does anyone know what the correct syntax is?

Rutty answered 26/8, 2013 at 13:53 Comment(2)
have you tried: '{{ $lic->CompanyName }}' ??Bignonia
@Bignonia - didn't work, same as the others.Rutty
R
0

It was my fault, I eventually used:
"<?php echo $lic->CompanyName ?>"
{{ $lic->CompanyName }} also works, once I'd corrected the underlying problem (a case-sensitivity issue).

Using Fiddler (which I cannot recommend highly enough) I found the blank browser page (just a white page) was caused by the following being returned in my page:

var data = 
[
    @foreach($licdata as $lic)
    {
    'label': "<!DOCTYPE html
<html>
  <head>

followed by a load of html (head tag, meta tags, css, etc...).

This is a result of the Field name (Companyname) in my SQL having slightly different case.
MS SQL Server doesn't care about the case, by PHP does (Companyname vs CompanyName)!

Once php/laravel creates objects out of the result set, the fieldnames become properties and when you access them, they MUST match up in case.

Always check the case of your PHP variables/objects, especially if a framework is auto-translating them from records to objects (or something similar).

Rutty answered 27/8, 2013 at 7:4 Comment(0)
P
8

A better solution on the backend is to structure your data in an array, associated or otherwise.

Then return it:

return view( 'view_required',[
     'my_var'=> json_encode($array_or_object, JSON_UNESCAPED_SLASHES );
]);

on the front:

<script>
    var my_var = {!! $my_var !!};
</script>
Profound answered 8/2, 2017 at 19:6 Comment(1)
My issue is that I was using {{ $my_var }} instead of {!! $my_var !!};Hydrophobic
E
1

Easyway https://laravel.com/docs/8.x/blade#rendering-json

<script>
     var app = {{ Js::from($array) }};
</script>
Escobedo answered 9/2, 2022 at 12:43 Comment(0)
R
0

It was my fault, I eventually used:
"<?php echo $lic->CompanyName ?>"
{{ $lic->CompanyName }} also works, once I'd corrected the underlying problem (a case-sensitivity issue).

Using Fiddler (which I cannot recommend highly enough) I found the blank browser page (just a white page) was caused by the following being returned in my page:

var data = 
[
    @foreach($licdata as $lic)
    {
    'label': "<!DOCTYPE html
<html>
  <head>

followed by a load of html (head tag, meta tags, css, etc...).

This is a result of the Field name (Companyname) in my SQL having slightly different case.
MS SQL Server doesn't care about the case, by PHP does (Companyname vs CompanyName)!

Once php/laravel creates objects out of the result set, the fieldnames become properties and when you access them, they MUST match up in case.

Always check the case of your PHP variables/objects, especially if a framework is auto-translating them from records to objects (or something similar).

Rutty answered 27/8, 2013 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.