How to display gridview based on current user login at index? For example, student A login and fill the form and logout. When other student login, the data that has been filled by student A will not display.
At gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'education_id',
'year',
'fieldstudy',
'institute',
'grade',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
At controller
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Education::find()->
where(['user_id' => Yii::$app->user->identity->id ]),
]);
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
}
At model
class Education extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'education';
}
public function rules()
{
return [
[['year', 'fieldstudy', 'institute', 'grade'], 'required'],
[['user_id'], 'integer'],
[['year', 'fieldstudy', 'institute', 'grade'], 'string', 'max' => 255],
];
}
public function attributeLabels()
{
return [
'education_id' => 'Education ID',
'student_id' => 'Student ID',
'year' => 'Year',
'fieldstudy' => 'Fieldstudy',
'institute' => 'Institute',
'grade' => 'Grade',
];
}
public function getId($id)
{
return static::find(['user_id' => $id]);
}
If you need retrive the Education info related to an user id and assuming that in the Education model this relation is based on the field user_id you could use Yii::$app->user->identity->id
$dataProvider = new ActiveDataProvider([
'query' => Education::find()->
where(['user_id' => Yii::$app->user->identity->id ]),
]);