PHP Warning – yii\base\ErrorException --Need Help

Hi,
I am getting following error on multiple pages when clicked.
The most common error are as following only third error changes based on where I click but first two error are always common. Usually 3-4 error per page…

PHP Warning – yii\base\ErrorException
Invalid argument supplied for foreach()
1. in /home/domains/domainname.com/public_html/frontend/views/layouts/main.phpat line 313
304305306307308309310311312313314315316317318319320321322<?php if (!empty(Yii::$app->user->id)) { ?>
    <div id="sidebar-wrapper" class="sidebar-wraper">
    <?php } else { ?>
        <div id="sidebar-wrapper">
        <?php } ?>
        <div class="sidebar-menu-listng">
            <ul class="nav navbar-nav sidebar-nav d-flex flex-column">
                <!-- Hot fixes -->
                <?php $categorypriority = yii::$app->Myclass->getCategoryPriority(); ?>
                ***<?php foreach ($categorypriority as $key => $category) :***
                    if ($category != "empty") {
                        $getcatdet = yii::$app->Myclass->getCatDetails($category);
                        $getcatimage = yii::$app->Myclass->getCatImage($category);
                        $subCategory = yii::$app->Myclass->getSubCategory($category);
                        ?> 
                        <!-- mobile category view-->
                        <li class="dropdown  main-mobile-menu">
                            <?php if (!empty($subCategory)) {  ?>
                                <a class="dropdown-toggle" data-toggle="dropdown" href="#"  style="background:url(<?php echo Yii::$app->urlManagerBackEnd->baseUrl . '/uploads/' . $getcatimage; ?>) no-repeat scroll 18px 17px / 32px auto; " >
2. in /home/domains/domainname.com/public_html/frontend/views/layouts/main.php at line 313– yii\base\ErrorHandler::handleError()
307308309310311312313314315316317318319        <div id="sidebar-wrapper">
        <?php } ?>
        <div class="sidebar-menu-listng">
            <ul class="nav navbar-nav sidebar-nav d-flex flex-column">
                <!-- Hot fixes -->
                <?php $categorypriority = yii::$app->Myclass->getCategoryPriority(); ?>
                ***<?php foreach ($categorypriority as $key => $category) :***
                    if ($category != "empty") {
                        $getcatdet = yii::$app->Myclass->getCatDetails($category);
                        $getcatimage = yii::$app->Myclass->getCatImage($category);
                        $subCategory = yii::$app->Myclass->getSubCategory($category);
                        ?> 
                        <!-- mobile category view-->
3. in /home/domains/domainname.com/public_html/vendor/yiisoft/yii2/base/View.php at line 348– require('/home/domains/domainname.c...')
4. in /home/domains/domainname.com/public_html/vendor/yiisoft/yii2/base/View.php at line 257– yii\base\View::renderPhpFile()
5. in /home/domains/domainname.com/public_html/vendor/yiisoft/yii2/base/Controller.php at line 425– yii\base\View::renderFile()
6. in /home/domains/domainname.com/vendor/yiisoft/yii2/base/Controller.php at line 411– yii\base\Controller::renderContent()
7. in /home/domains/domainname.com/public_html/frontend/controllers/MessageController.php at line 833– yii\base\Controller::render()
827828829830831832833834835836837838839    }
    public function actionHelp($details){
        if (!empty($details)){
            $helppageModel = Helppages::find()->where(['slug'=>$details])->one();
            if (!empty($helppageModel)){
                $allhelppageModel = Helppages::find()->all();
                **return $this->render('help',['helppageModel' => $helppageModel,'allhelppageModel' => $allhelppageModel]);**
            }else{
                Yii::$app->session->setFlash('error',"Unable to Process our request");
                $this->redirect(array('/'));
            }
        }else{
            Yii::$app->session->setFlash('error',"Unable to Process our request");
8. frontend\controllers\MessageController::actionHelp()
9. in /home/domains/domainname.com/public_html/vendor/yiisoft/yii2/base/InlineAction.php at line 57– call_user_func_array()
10. in /home/domains/domainname.com/public_html/vendor/yiisoft/yii2/base/Controller.php at line 181– yii\base\InlineAction::runWithParams()
11. in /home/domains/domainname.com/public_html/vendor/yiisoft/yii2/base/Module.php at line 528– yii\base\Controller::runAction()
12. in /home/domains/domainname.com/public_html/vendor/yiisoft/yii2/web/Application.php at line 104– yii\base\Module::runAction()
13. in /home/domains/domainname.com/public_html/vendor/yiisoft/yii2/base/Application.php at line 392– yii\web\Application::handleRequest()
14. in /home/domains/domainname.com/public_html/frontend/web/index.php at line 165– yii\base\Application::run()
159160161162163164165    require __DIR__ . '/../../common/config/main.php',
    require __DIR__ . '/../../common/config/main-local.php',
    require __DIR__ . '/../config/main.php',
    require __DIR__ . '/../config/main-local.php'
);
 
***(new yii\web\Application($config))->run();***
$_GET = [
    'details' => 'reach',
];

$_COOKIE = [
    'gc_first_page_url' => 'https://domainname.com/',
    'gc_first_page_referrer' => '',
    'gc_first_page_timestamp' => '1725059465648',
    'G_ENABLED_IDPS' => 'google',
    '_identity' => '654bf34e62a3125e54bb4767e217797bb52e235a7165001342c7598f657f581aa:2:{i:0;s:9:"_identity";i:1;s:47:"[19,"eyeHkD84m4u__AFH7kV0_Nbm3syBWZPD",2592000]";}',
    '__stripe_mid' => '275ac452-44c2-463a-95b1-6cdfebe0d7d9843eca',
    'PHPSESSID' => 'monvon44qsia2k51mobvkisv4v',
    'session' => 'VP4F4OFNRNAL3UU46HODHADDK4BCNKXBTDXH37A',
    '_csrf-frontend' => '48ade04b55ad3499d462136c67f2a934a72383875642ecdc4c26bfcb8091b06ba:2:{i:0;s:14:"_csrf-frontend";i:1;s:32:"JV722atKBPo-abZ4XrKtAMtsMEK86I47";}',
    '__stripe_sid' => 'ffc965b2-b40b-41d2-ab68-8754cd63dd4ad38414',
];

$_SESSION = [
    '__flash' => [],
    '__id' => 19,
    'distance' => '200',
    'language' => 'en',
    'latitude' => '23.804093',
    'longitude' => '90.4152376',
    'curr_latitude' => '23.804093',
    'curr_longitude' => '90.4152376',
    'place' => 'Dhaka, Bangladesh',
    'curr_place1' => 'Dhaka, Bangladesh',
];
Yii Framework
2024-09-10, 04:04:29

Apache/2

Yii Framework/2.0.39.3```
As you can see the fist 2 code begin with *** code and last one is always common. The 3rd begin with ** line changes based on where I click.
If there any way to fix. I am using PHP 7.4 and Yii Framework 2.0.39.3

Invalid argument supplied for foreach() occurs when you try to use foreach loop on a variable that is not an array of object.
Try to check if the variable is an array of object

$values = getValues(); // Assume this function might return non-array values

if (is_array($values) || is_object($values)) {
    foreach ($values as $value) {
        // Process each value
    }
} else {
    echo "The variable is not an array or object.";
}

may this helps.