Answer the question
In order to leave comments, you need to log in
Yii2 REST: How to correctly process a post request that passes a json collection?
I apologize in advance if the questions are too stupid, but everyone started somewhere.
I need to get data from a post request, which is passed as a json collection.
The collection looks like this:
[{ "title":"test1", "description":"test1", "date":"17.01.12 11:11:11" }, {"title":"test2", "description":"test2", "date":"17.01.12 22:22:22" }]
Answer the question
In order to leave comments, you need to log in
Get post data, decode it into an array, traverse the array and load the data into the model and save it. Let's say you pass data in the post parameter arr and your model is called Model
$data = json_decode(\Yii::$app->request->post('arr'), true);
foreach ($data as $chunk) {
$model = new Model();
$model->attributes = $chunk;
$model->save();
}
if (!$model->save()) {
var_dump(['erorrs' => $model->errors]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question