skip to Main Content

I have two tables (Entities, Division) like this

Entities

id name is_freezone
1 ABC LTD 0
2 A & B LLC 0
3 C & D LLC 0

Divisions

id name entity_id
1 TOYS 2
2 FOOD 2
3 HOUSEHOLD 3

I want the query to get the name of entity with the foriegn key (entity_id). I want the data to be displayed like :

id name entity_name
1 TOYS A &B LLC
2 FOOD A & B LLC
3 HOUSEHOLD C & D LLC

My code :


        $db = ConfigDatabase::connect();
        $builder = $db->table('divisions');
        $builder->select('*');
        $builder->join('entities', 'divisions.entity_id = entities.id');
        $query = $builder->get()->getResult();

But this not returning the expected result. Can anyone can help me to achieve this ?

Thanks

2

Answers


  1.     $db = ConfigDatabase::connect();
                $builder = $db->table('divisions');
                $builder->select('divisions.id as id, divisions.name as name, entities.name as entity_name');
                $builder->join('entities', 'divisions.entity_id = entities.id');
                $query = $builder->get()->getResult();
    
    //try this, hope it work
    
    Login or Signup to reply.
  2. you can use an alias to differentiate the repeated name field and debug the output
    print_r($query);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search