Reputation: 21
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 ?
Upvotes: 0
Views: 68
Reputation: 327
I always use this posts = {!! json_encode($posts) !!};
and it works fine!
Upvotes: 2
Reputation: 3065
Try this one,
var posts = "{{ $post->toJson() }}";
alert(posts);
Upvotes: 0