[Vuejs]-Right way to pass and store data from Laravel to Vue

0👍

It is not a redundancy problem, it’s a logic problem. You should not pass ANY variable from blade to view in my opinion. You should do something like this:

  1. Your controller (maybe from the index method) checks if the request wants a JSON response, if not returns the view, otherwise the collection as a JSON response (go on to understand why).
  2. When the view is loaded, VueJs loads the component and (this is how I do it) within the mounted method you make an Ajax call (maybe with axios) to your controller which will return the collection

In this way you have an asynchronous request management, that will allow you to refresh the data, and avoid to reload the page each time.

As I wrote before, I would avoid as much as possible to pass properties from blade to vue, unless they’re context variables like user preferences or system settings.

-2👍

Your code is ok. You can get categories over ajax. In your case, it is not recommended to use ajax.

Leave a comment