javascript

Manipulate Laravel Data with Javascript

And heres what I mean by that. Once you have successfully passed data, lets say for example your Posts::all(), to your blade view, like so.

$posts  = Posts::all();
return view('posts.edit', compact('posts'));

Then inside your blade view, at the bottom open a script and pass that data to JavaScript using JSON!

<script>
   const userId = {{ auth()->id() }};
   const postsData = @json($posts);
   const filteredPosts = postsData.filter(posts => posts.user_id == userId);
   console.log(filteredPosts)
</script>

And voila! Just like that, you have your data ready to be used in JavaScript! As you can see I just added a simple filter that only shows Posts that i've made, and added a console log to check them out! Pretty cool stuff!