Codeigniter 4 - 尝试读取 属性 "bannerimg" 上的布尔值,同时从数据库中获取图像

Codeigniter 4 - Attempt to read property "bannerimg" on bool while fetching images from database

所以我正在为我使用新的 CodeIgniter 4 的 Web 应用程序创建轮播。到目前为止,我已经成功地上传了图像,但是我似乎无法获取图像以在前端显示它。它给了我以下错误 - Attempt to read 属性 "bannerimg" on bool.

编辑 - 我正在尝试从数据库中获取所有图像(目前存储了 4 张图像)

我的数据库table

Database table for carousel - Please check this image too!

下面是代码

AdminModel中获取图片的函数

    //This functions gets the images from db
public function getBanner() {

                $builder = $this->db->table('tblbanner');
                $result = $builder->get();

                if(count($result->getResultArray()) == 1) {

                        return $result->getRow();

                } else {

                        return false;
                        
                }

        }

管理员控制器

public function banners() 

    {
        if(!session()->has('logged_staff')) {

                return redirect()->to(base_url(). "/team");
    
        }
        $data = [];
        $data['validation'] = null;
        $suid = session()->get('logged_staff');

        $data['getad'] = $this->adminModel->getBanner(); // This line of fetches the images from database.
        $data['staffdata'] = $this->adminModel->getLoggedStaffData($suid);

        echo view("team/Templates/header_panel");
        echo view("team/navigation", $data);
        echo view("team/sidebar", $data);
        echo view("team/banners", $data);
        echo view("team/Templates/footer_panel");
    }

查看文件

        <div class="card-body">
                          <div class="table-responsive">
                                <table class="table table-bordered table-striped" id="jsdata">
                                <thead>
                                  <tr>
                                    <th class="text-center">Edit Image</th>
                                    <th class="text-center">Ad Image</th>
                                    <th class="text-center">Upload Date</th>
                                    <th class="text-center">Delete Image</th>
                                  </tr>
                                </thead>
                                <tbody>
                                  <tr>
                                    <td class="text-center"><a class="btn bg-success"><i class="fas fa-edit"></i></a></td>
                                    <td class="text-center">

    **// The image fetched from database is displayed here //**

                                    <?php if($getad->bannerimg != '') : ?>
                                      <img class="profile-user-img img-fluid" src="<?= $getad->bannerimg; ?>" alt="Ad">
                                      <?php else : ?>
                                      <img class="profile-user-img img-fluid" src="<?= base_url(); ?>/public/assets/img/no-image.png" alt="Ad">
                                      <?php endif; ?>

   **// The image fetched from database is displayed here //**

                                    </td>
                                    <td class="text-center"><?= $getad->uploaded; ?></td>
                                    <td class="text-center"><a class="btn bg-danger"><i class="fas fa-trash"></i></a></td>
                                  </tr>
                                </tbody>
                              </table>
                          </div>
                      </div>
                      <!-- /.card-body -->
                    </div>

如果您在 table 中有不止一张图片,那么这将 return false:

            if(count($result->getResultArray()) == 1) {
                    return $result->getRow();
            } else {
                    return false;                        
            }

所以 $data['getad'] 并且在视图中 $getad 将是 false。此外,您获取一个数组,然后尝试将其作为对象访问 $getad->bannerimg.

因为你只想要一行,去掉 if 语句,只需要 return 一个对象:

return $result->getRow();

然后在您的模板中,只需:

<?php if(!empty($getad->bannerimg)) : ?>