Codeigniter db_where in 3 joined tables - codeigniter

So this code is a combination of 3 table joined, now i want to filter the query where only the request that a user can have is his/her own request and not from other user.. the problem is in where clause i have duplicated.
$this->db->order_by('loanrequest.ApplicationNo', 'DESC');
$query = $this->db->get('loanrequest','loanapplication','user');
$this->db->join('loanapplication','loanrequest.ApplicationNo = loanapplication.ApplicationNo');
$this->db->join('user', 'user.userId = loanapplication.userId');
$this->db->where('loanapplication.userId', $this->session->userdata('userId'));
$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
return $query->result_array();

I can not get what you have done in your join query but Here is how I do join in my CodeIgniter model.
You don't need to put this in where condition:
$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
Because you have already done that in the join query.
$query = $this->db->select('*')
->from('loanapplication')
->join('loanrequest', 'loanrequest.ApplicationNo = loanapplication.ApplicationNo', 'left')
->join('user', 'user.userId = loanapplication.userId', 'left')
->where_in('loanapplication.userId',$this->session->userdata('userId'))
->order_by('loanrequest.ApplicationNo', 'DESC')
->get();
return $query->result_array();
I will recommend you should use $this->db->last_query() because it gives you a better idea about your query.

$query = $this->db->select('*')
->from('loanapplication')
->join('loanrequest', 'loanrequest.ApplicationNo = loanapplication.ApplicationNo', 'left')
->join('user', 'user.userId = loanapplication.userId', 'left')
->where_in('loanapplication.userId',$this->session->userdata('userId'))
->order_by('loanrequest.ApplicationNo', 'DESC');
$result = $query->get();
echo $this->db->last_query();exit; //use this to print your query so that you can get the actual issue
if ($result->num_rows() > 0) {
return $result->result_array();
} else
return '';

Related

Laravel query groubBy condition

I have a dB query where I would like to groupBy() only when conditions are met without using union because of pagination.
Unfortunately groupBy() seems to only work when called on the entire query outside of the loop.
This was made for dynamic filtering from $filterArr. Depending on the array I need to select from different columns of the table.
When the $key=='pattern' I would need the distinct results from its column.
the query looks something like this
select `col_1`, `col_2`, `col_3`
from `mytable`
where (`color` LIKE ? or `pattern` LIKE ? or `style` LIKE ?)
group by `col_2` //<< i need this only for 'pattern' above and not the entire query
Heres the model:
// $filterArr example
// Array ( [color] => grey [pattern] => stripe )
$query = DB::table('mytable');
$query = $query->select(array('col_1', 'col_2', 'col_3'), DB::raw('count(*) as total'));
$query = $query->where(function($query) use ($filterArr){
$ii = 0;
foreach ($filterArr as $key => $value) {
if ($key=='color'){
$column = 'color';
}else if ($key=='style'){
$column = 'style';
}else if ($key=='pattern'){
$column = 'pattern';
$query = $query->groupBy('col_2'); // << !! does not work
}
if($ii==0){
$query = $query->where($column, 'LIKE', '%'.$value.'%');
}
else{
$query = $query->orWhere($column, 'LIKE', '%'.$value.'%');
}
$ii++;
}
});
$query = $query->orderBy('col_2', 'asc')->simplePaginate(30);
I think you can simplify your code a bit:
$query = DB::table('mytable');
$query = $query->select(array('col_1', 'col_2', 'col_3'), DB::raw('count(*) as total'));
$query = $query->where(
collect($filterArr)
->only(['color','style','pattern'])
->map(function ($value, $key) {
return [ $key, 'like', '%'.$value.'%', 'OR' ];
})->all()
)->when(array_key_exists('pattern', $filterArr), function ($query) {
return $query->groupBy('col_2');
});
$query = $query->orderBy('col_2', 'asc')->simplePaginate(30);

How to convert this SQL Query on Code Igniter Model

Good Day Masters,
Can anyone help me how to convert this SQL Query into Code Igniter format (model).
SELECT firstName, FLOOR(DATEDIFF(CURRENT_DATE, birthDate)/365.25) as age FROM residents_tbl WHERE FLOOR(DATEDIFF(CURRENT_DATE, birthDate)/365.25) >= 18
I don't know how to write it on WHERE clause.
$query = $this->db->select('*');
$query = $this->db->from('residents_tbl');
**$query = $this->db->where('isHead', '1');**
$query = $this->db->order_by('lastName', 'ASC');
$query = $this->db->get('', 15, $this->uri->segment(3));
if ($query->num_rows() > 0) {
return $query->result();
}
TIA.
This is a simplified version with chaining. I just changed the type of 1 from string to number which might caused the problem.
$query = $this->db
->where('isHead', 1)
->get('residents_tbl')
->order_by('lastName', 'ASC');

how to fetch all records from order table in Codeigniter

$data is an array which contain user post data for fetch record from orders table
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
$data = array(
'customer_id' => $this->input->post('custId')],
'paided' => 2
);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
try this :
public function function_name (){
$data = array (
'customer_id' => $this->input->post('custId'),
'paided' => 2
);
$this->db->select('*');
$this->db->from('ordere');
$this->db->where($data);
$query = $this->db->get();
return $query->result_array();
}
You have done all good just need to put result() if you get multiple row or row() if you get one row
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$result= $this->db->get()->result(); //added result()
print_r($result);
as simple use
$custId = $_post['custId'];
$query = $this->db->query("SELECT * FROM orders WHERE customer_id= '$custId' AND paided='2'");
$result = $query->result_array();
return $result;//result will be array
This is the plus of using framework, you don't need to write that much of code,
$where = array('customer_id' => $this->input->post('custId'),'paided'=>2)
$result = $this->db->get_where('orders', $where);
and for fetching them, use $result->row() for single record retrieval.
If you want all records, use $result->result()
Here is documentation link, if you want to learn more.
What You should need to be Correct And Why
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*'); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$this->db->get(); // this will not return data this is just return object
So Your Code Should be
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select(); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$query = $this->db->get();
$data = $query->result_array();
// or You can
$data= $this->db->get()->result_array();
here result_array() return pure array where you can also use result()
this will return array of object

Translate from CodeIgniter Active Record model to Laravel Eloquent

I am a CodeIgniter trying to adopt Laravel, however, I have been having a lot of problems understanding how to use Eloquent.
I suspect that if I could figure out how to translate some of my CodeIgniter Model methods to Laravel Eloquent I might be able to get on better. Hopefully, this will help others with the same problem.
Could anybody please rewrite the following from CodeIgniter to Eloquent:
public function get_products($product_id = NULL, $category_id = NULL, $limit = NULL)
{
$this->db->select('*, product.id AS product_id');
$this->db->from('product');
$this->db->join('product_unit', 'product.source_unit_id = product_unit.id', 'left');
$this->db->join('stock_levels', 'product.stock_level_id = stock_levels.id', 'left');
$this->db->join('categories', 'product.category_id = categories.cat_id', 'left');
if(isset($product_id)) {
$this->db->where('product.id', $product_id);
}
if(isset($category_id)) {
$this->db->where('product.category_id', $category_id);
}
if(isset($limit)) {
$this->db->limit($limit);
}
#$this->db->order_by('categories.cat_name', 'ASC');
$this->db->order_by('categories.cat_name', 'ASC');
$this->db->order_by('product.name', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
Here's an approximate version of your query, there should be some things to tweak, but I hope you get the idea:
public function get_products($product_id = NULL, $category_id = NULL, $limit = NULL)
{
$query = Product::leftJoin('product_unit', 'source_unit_id', '=', 'product_unit.id')
->leftJoin('stock_levels', 'stock_level_id', '=', 'stock_levels.id')
->leftJoin('categories', 'category_id', '=', 'categories.cat_id');
if(isset($product_id)) {
$query->where('product.id', $product_id);
}
if(isset($category_id)) {
$query->where('product.category_id', $category_id);
}
if(isset($limit)) {
$query->limit($limit);
}
#$this->db->order_by('categories.cat_name', 'ASC');
$query->orderBy('categories.cat_name', 'ASC');
$query->orderBy('product.name', 'ASC');
dd( $query->toSql() ); /// this line will show you the sql generated and die // remove it to execute the query
return $query->get()->toArray();
}

How can use order-by in codeigniter?

I know this is simple but i didn't complete it.
$query = $this->db->get_where('prepared_forms', array('topic' => $this->input- >post('prepared_topics')));
$new_form = $query->row_array();
How can order prepared forms order by topic name (ASC)?
$this->db->select("*")
->from('prepared_forms')
->where('topic', $this->input->post('prepared_topics'))
->order_by('topic', 'asc')
->get()
->result_array();
Try this:
$query = $this->db->order_by('topic', 'asc')->get_where('prepared_forms', array('topic' => $this->input->post('prepared_topics')));
$new_form = $query->row_array();
$this->db->order_by("topic", "asc");
$query = $this->db->get_where('prepared_forms', array('topic' => $this->input->post('prepared_topics')));
$new_form = $query->row_array();

Resources