S
S
Sergey2020-06-09 23:08:43
API
Sergey, 2020-06-09 23:08:43

What is the best way to get data for create/edit forms?

Laravel + Vue - but this is not essential for the question
How to correctly (best practices) form creation / editing forms in a SPA application when there are select elements in the forms with data from different database tables?
For example, in the Users form, fields with the choice of role / company / city / etc. are needed. Accordingly, when displaying the form, it is necessary to load the data for these selects.
In non-SPA applications, I used the Create - Edit methods in the controllers and returned the rendered Blade with all the data.
In SPA, the backend acts as an api, and when registering routes

// routes\api.php
Route::apiResource('users', 'UserController');

only index, store, destroy, update, show routes are created - which makes you think that you need to receive data for forms in some other way.
Option 3:
1: Use the old method and add the Create and Edit methods to the controller, and return all the data necessary for the forms in them (why then, by default, these routes were not added to the api controllers)
2: For each select, make a separate request to get the list options to the appropriate controller (too many requests).
3: Use, for example, Vuex and store all the necessary data in the store, loading them as needed (but what if, for example, the list of roles has changed, and when you open the form, the data about the Roles in the store will be outdated)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question