R
R
Romi2021-08-20 09:24:39
OOP
Romi, 2021-08-20 09:24:39

What is the best practice for converting incoming JSON when creating/updating a model via an API, where is the custom to do so?

Essence of the question:

When creating a model, we get on / api / create JSON in which there are 100500 variables, which then need to be somehow tied to the model, supplemented with other data, and saved.

It turns out such a sheet

$user->name = $input['name'];
$user->address = $input['address'];
...
$user->babushka = $input['babushka'];

$user->save();


the same and on /api/update

In general, the controller consisting of several fattest methods turns out.

In principle, everything works, but I want to know what is the Best practice for assembling such objects.

Where is it common to do this - leave it in the controller? Remove to model? Create some special class?

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2021-08-20
@romicohen

$user->update($request->validated())
for creation similar to create

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question