Trying to access array offset on value of type null error solution

How to solve the issue Trying to access array offset on value of type null error solution .Below is my query what is wrong in this query

$user = ArrayHelper::map(User::find()
->where([‘between’,‘role_id’,$start,$end])
->orderBy([
‘username’ => SORT_ASC,
])
->with(‘enrollment’,‘role’)
->all(), ‘id’, function ($model) {
return $model[‘enrollment’][‘adhar_name’] . ’ - ’ . $model->username.’ (’.$model[‘role’][‘name’].’)’;});

Assuming your code is formatted without the backticks (which it must be or you would have a syntax error) - like this:

$user = ArrayHelper::map(
    User::find()
        ->where(['between','role_id',$start,$end])
         ->orderBy(['username' => SORT_ASC])
         ->with('enrollment','role')
         ->all(), 'id', function ($model) {
             return $model['enrollment']['adhar_name'] . ' - ' . $model->username.' ('.$model['role']['name'].')';
         }
);

Then I don’t think there is anything wrong with your code, but more likely to be the possibility that one/or more of your database records does not match the criteria that your query is built upon.

So in other words, the model relationships that you have created to both “enrollment” and “role”, one of your users does not match and therefore does not return a value.

So the error you are seeing " Trying to access array offset on value of type null" is because one of your array keys has no value, so it’s either:

$model['enrollment']['adhar_name']
or
$model['role']['name']

You might be able to reduce the volume of records returned using your variables $start and $end until you find out who has the issue - and as I mentioned above it could be more than one.