CodeIgniter 4 Query Builder Join


CodeIgniter 4 Query Builder Join

CodeIgniter 4 Query Builder Join, How to do a table join in a few steps using the codeigniter framework


CodeIgniter 4 Query Builder Join – Example

The CodeIgniter framework in its version 4 has offered a new way of working with web applications compared to its old versions 2 and 3, in this post we will see a way to relate tables with the query generator since until now it does not have a option to relate tables with your ORM, we add the following code

public function join_example()
	{

		$db      = \Config\Database::connect();
		$builder = $db->table('authors AS A');
		$builder->select('B.book_id,B.book');
		$builder->join('books AS B', 'A.author_id = B.author_id');
		$builder->where('A.author_id', $author);
		$query = $builder->get()->getResult();

		return $this->response->setJSON($query);
		
	}

In this example we propose an exercise with a table A called authors and a table B called books, where a cross of tables will be performed by their unique identifier, to finally return a response in json format