How to show records created by that users on index page in yii2?

I am using yii2 basic and implemented RBAC.

I have two roles admin and fieldofficer and created permissions and rules and assigned users. Now when admin logs in, on index page he should be able to see all records as well as his created records in grid.

Whereas when fieldofficer logs in, he should be able to see only his created records in index page.

How to accomplish this?

Create ‘seeAll’ permission and grant it to ‘admin’ role.

    public function actionIndex()


        $searchModel = new ModelSearch();

        if (!Yii::$app->user->can('seeAll')) {

            $searchModel->created_by = Yii::$app->user->id;


        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,



Now the users without ‘seeAll’ permission can see only the records that they have created themselves. Note that the ‘search’ method will filter the results by ‘created_by’ attribute of the model.