V
V
Vyacheslav2020-12-20 18:19:51
Yii
Vyacheslav, 2020-12-20 18:19:51

How to display a list of posts with 5 comments for each (if any) in Yii2 in RESTfull?

There are 2 tables in the database Post and Comment. Connected through one to many.
When calling actionIndex in the controller, it is necessary to display all posts but with comments added to each post in response. the number of comments should be limited (for example, 5).
How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Lykasov, 2020-12-21
@lykasov-aleksandr

Make your actionIndex in the controller with the request logic you need. Something like this:

class PostController extends Controller
{
    public function actionIndex()
    {
        return new ActiveDataProvider([
            'query' => Post::find()->joinWith('comments')->...чего-то-там-ещё,
        ]);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question