1 year ago

#77394

test-img

Anonymous

How to join two tables with ActiveRecord methods in CodeIgniter 4?

I have some data submitted into db_table(e.g lodge), some of which are id_no from another table(e.g room_type). When I try to fetch data from the db_table(lodge) to my view which comes in array, it returns the id value(room_type_id) submitted which is fine. But the goal is to print the room_type_name from the room_type table. How do I do this?

My Controller

public function invoice($lodge_no = '') {
    $data = [
        'page_name' => 'Invoice',
        'page_title' => 'Invoice | The Ashokas',
        'invoice_data' => $this->lodgeModel->fetchInvoiceData($lodge_no)
    ];
    return view('lodge/invoice', $data);
}

My Model

public function fetchInvoiceData($lodge_no) {
    $builder = $this->db->table('lodge');
    $builder->where('lodge_no', $lodge_no);
    $result = $builder->get();
    if (count ($result->getResultArray()) > 0) {
        return $result->getRowArray();
    }else{
        return false;
    }
}

My View

<td class="text-center">
    <div><strong>Room No:</strong></div>
    <div class="text-muted"><?= $invoice_data['room_type_id'] ?> - Room <?= $invoice_data['room_id'] ?></div>
</td>

My Views print Room No: 5 - Room 5 The first 5 is the room_type_id but I want it to display the room_title(Hibiscus) assigned with this id 5 in the room_type database as shown in the database image below enter image description here

Lodge Database enter image description here

php

codeigniter

join

0 Answers

Your Answer

Accepted video resources