将数据从laravel控制器传递给Js

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 ?

Try this one,

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

alert(posts);

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