0

I'm having trouble I f I try to use field that are named with "AS" Keyword and If I change "protected $table = "another table", All I get is "Undefined index: total" error message "Undefined index: fel" error message. I don't know how to do to fix this issue. Help please.

Thanks in advance.

//My Model

        protected $table = 'churchmembers';

        public function getMembers()
        {
            $this->db->table("churchmembers")
                     ->select('
                            fel.felId, 
                            fel.fel, 
                            (
                                COALESCE(SUM(churchmembers.children),0)+ 
                                COALESCE(SUM(churchmembers.adults),0) 
                            ) AS total
                         ')
                      ->join('fel','ON fel.felId = churchmembers.felId');
             }
             return $this;
        }

//My Controller

        public function index()
        {
            $data['result'] = $this->model->getMembers()->paginate(5);
            $data['pager']  = $this->model->pager;
            return view('Home/index', $data);
        }

//My Views

        <?php foreach($result as $row): ?>
            <div><?= $row['felId'] ?></div>
            <div><?= $row['fel'] ?></div>
            <div><?= $row['total'] ?></div>
        <?php endforeach; ?>
ven
  • 175
  • 9
  • You have defined the $table and never used it. At least I can't see that in your code in the model. – Maroof Feb 22 '21 at 04:19
  • Thank you, My Query was wrong I fixed it. – ven Feb 22 '21 at 06:30
  • But I have the same problem If I use $sql = "SELECT t.*, b.* FROM t JOIN b ON b.id = t.id"; $this->db->query($sql); return $this; and print to my controller print_r($this->model->myMethod()->asObject()->paginate(4));. Only I get is data of the $table and ignored data of the joined table, How can I fix that? – ven Feb 22 '21 at 06:37
  • Please here is my question that has a similar issue https://stackoverflow.com/questions/66311594/how-to-get-data-of-joined-table-using-query-method – ven Feb 22 '21 at 07:32

0 Answers0