1

Laravel code :

$teachers = Teachers::where('possessed_by_community', $communityId)->pluck('teacher_name');

return view('pages.show_add_teachers', [
    'teachers'  => $teachers
]);

Then in client side I tried :

var teachers = "<?php echo json_encode($teachers) ?>" ;
teachers = JSON.parse(teachers);
console.log(teachers);

In webconsole I get :

SyntaxError: missing ; before statement

In firefox debugger :

enter image description here

2
  • 2
    use single quotes instead var teachers = '<?= json_encode($teachers) ?>'; Commented Sep 9, 2017 at 9:09
  • @LawrenceCherone, that solved. Thank you. Commented Sep 9, 2017 at 9:11

2 Answers 2

3

Use single quotes instead var teachers = '<?= json_encode($teachers) ?>';

or don't use quotes at all, leaving out JSON.parse(teachers);.

var teachers = <?= json_encode($teachers) ?>;
console.log(teachers);

Though you must check $teachers is valid, string, null or array at all times.

Else it would become var teachers = ; and break.

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

1 Comment

var teachers = '<?php echo json_encode($teachers) ?>' ; teachers = JSON.parse(teachers); worked
0
var jArray = @json($teachers);

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

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.