Alex Tsirikidze
Alex Tsirikidze

Reputation: 21

Passing data from laravel controller to 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 ?

Upvotes: 0

Views: 68

Answers (2)

nyu.exe
nyu.exe

Reputation: 327

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

Upvotes: 2

Md.Sukel Ali
Md.Sukel Ali

Reputation: 3065

Try this one,

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

alert(posts);

Upvotes: 0

Related Questions