1

View

Right now, when I do

<pre>{{ $device->window }}</pre>

I see this

enter image description here


I want to access it in JS. Ex. device.screen.height, It's 926

I've tried

console.log(`{{ json_decode($device->window) }}`);
console.log(JSON.parse(`{{ json_decode($device->window) }}`));
console.log(JSON.parse(`{{ json_decode($device->window) }}`));
console.log(JSON.stringify(`{{ json_decode($device->window) }}`));
console.log(JSON.stringify(`{{ json_decode($device->window) }}`));
console.log(JSON.parse(`{{ json_encode($device->window) }}`));
console.log(JSON.parse(`{{ json_encode($device->window) }}`));
console.log(JSON.stringify(`{{ json_encode($device->window) }}`));
console.log(JSON.stringify(`{{ json_encode($device->window) }}`));

I kept getting

htmlspecialchars() expects parameter 1 to be string, object given


If I do :

console.log(JSON.parse(JSON.stringify({{ json_encode($device->window) }})));

I got

&quot;{&quot;innerWidth&quot;:&quot;980&quot;,&quot;innerHeight&quot;:&quot;1708&quot;,&quot;devicePixelRatio&quot;:&quot;3&quot;,&quot;screen&quot;:{&quot;width&quot;:&quot;428&quot;,&quot;height&quot;:&quot;926&quot;}}&quot;

I got so many &quot;

9
  • Decode makes it useable for PHP, encode allows you to pass it to javascript as a string. Since it's already encoded, don't mess with it on the PHP side Commented Feb 15, 2022 at 21:03
  • PHP side, I passed it to JS (FE) as is. Let me try encoding then. If you know the right syntax please feel free to suggest. Commented Feb 15, 2022 at 21:06
  • 1
    I would suggest you not to do that. It is not a common nor good practice. Mixing languages usually end up bad. Better to get the data in another way like AJAX. But if you insists, you may do some good with json encode and decode Commented Feb 15, 2022 at 21:09
  • I am using Laravel blade, it's the details page of my device. In that I already have access to $device object base on id. Commented Feb 15, 2022 at 21:10
  • I can access any attribute like $device->propertyName I don't need to do another AJAX for that. Commented Feb 15, 2022 at 21:11

3 Answers 3

1

You have to use different sintax for that :

{!! json_encode($device->window) !!}

But you should not access it like this. Its not good practice to mix languages.

Sign up to request clarification or add additional context in comments.

5 Comments

YOUR suggestion works perfectly on first try, may I know this support on all Laravel version ? Can you please attach a ref link, so I can read more about it.
How do I access my height ? .screen.height not working.
please acept my answer if it worked for you so topic will be closed @code8888
Here is answer for screen height : stackoverflow.com/questions/3437786/…
1

Inside a script tag, you could do:

const myData = {{ $device->window }};
console.log(myData.screen.height);

2 Comments

let may suit $device->window better than const, as these are not constant values. PHP will deliver only the initial value anyway - therefore the whole question is kind of questionable.
1

The's a blade directive for that:

@php
    $json = [ 1, new DateTime, (object)[], 'a' ];
@endphp
<script>
    test = @json($json);
    console.log(test)
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.