$teachers = User::where('profile_type', 'App\Models\TeacherProfile')->get()->toArray();
<script>
var jsonData = @json($teachers);
console.log(jsonData);
</script>
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm returning a json to my blade view then I'm passing it to javascript. But I'm getting an error: Uncaught SyntaxError: expected property name, got '&'
I don't know how a parsed json should look like, when I click on the error view source it looked like this
const jsonData = JSON.parse({"1":{"id":23434,"firstname":"Kerri","middlename":"Ellis","lastname":"Chelsey","email":"[email protected]","email_verified_at":null,"profile_type":"App\Models\TeacherProfile","profile_id":14,"created_at":"2021-10-22T16:28:00.000000Z","updated_at":"2021-10-22T16:28:00.000000Z","profile":{"id":14,"contact":"09295554077","created_at":"2021-10-22T16:28:00.000000Z","updated_at":"2021-10-22T16:28:00.000000Z"}}
I'm guessing I need to escape the &? but I'm not really sure and I don't know how.
This is my js code I was trying to check if I was getting the data.
<script>
const jsonData = JSON.parse({{ $teachers }});
console.log(jsonData);
</script>
$teachers = User::where('profile_type', 'App\Models\TeacherProfile')->get()->toArray();
<script>
var jsonData = @json($teachers);
console.log(jsonData);
</script>
Please or to participate in this conversation.