Yii2 - 用分页查询

I have a Category model that has a relation with the Products model. I am doing the following query:

$model = Category::find()->where(['slug' => $slug])->with('products')->one();

And in the view I can retrieve all the products that belong to that Category with:

foreach ($model->products as $value)...

Now I want to do the pagination in the products but I can not find how to do it in the "with" part of the query.

Try this

In Controller:

...
$pages = new Pagination(['totalCount' => $model->products->count()]);
return $this->render('index', [
         'model' => $model,
         'pages' => $pages,
    ]);

In View:

foreach ($model->products as $value) {
    // display $model here
}

// display pagination
echo LinkPager::widget([
    'pagination' => $pages,
]);