0

I have problem about passing data from controller to js, i am using this code for it ,i tried many things but nothing is working , am i doing something wrong ?

script:

var posts = {!! $post->toJson() !!};

alert(posts);

and my controller looks like this :

$post = Post::where('category',$id)->get();

but i am getting error "Uncaught SyntaxError: Unexpected token !" i really dont know why is there any solutions ?

1
  • Are you receiving data from $post? Commented Jan 9, 2019 at 16:47

2 Answers 2

2

I always use this posts = {!! json_encode($posts) !!}; and it works fine!

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

6 Comments

If this worked for you please check my answer as correct!
Maybe this "posts = {!! json_encode($posts) !!};" fix it for you!
nah same when Uncaught SyntaxError: Unexpected token !
writted it bad :S posts = "{!! json_encode($posts) !!}"; notice the " before the {!! and after !!}. This is weird bc I faced this bug more times and i've always fixed this way.
it just alerts string "{!! json_encode($posts) !!}" this
|
0

Try this one,

var posts = "{{ $post->toJson() }}";

alert(posts);

1 Comment

it alerts string

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.