fetch data from database based on date - codeigniter

This is my table fields
table1 : cash_transactions
id
dated_on
amount
from_user
to_user
trans_type
userid
purpose
table2 : login
loginid
name
username
password
usertype
status
Controller : Admin
public function daily_report()
{
$daily_date=date('Y-m-d');
$data['fetch']= $this->admin->get_daily_report($daily_date);
$this->load->view('daily_report',$data);
}
Model: Admin_Model
public function get_daily_report($daily_date)
{
$this->db->select('c.userid,c.amount,c.trans_type,c.from_user,c.to_user,c.purpose,l.loginid,l.name');
$this->db->from('cash_transactions as c');
$this->db->join('login as l', 'c.userid = l.loginid');
$this->db->where('c.dated_on',$daily_date);
$query = $this->db->get();
$result =$query->result();
return $result;
}
Here i want to fetch the current date data from the database, but this is not working. Please solve this problem.

you can try this:
Controller : Admin
public function daily_report()
{
$daily_date=date('Y-m-d');
$data['fetch']= $this->admin->get_daily_report($daily_date);
$this->load->view('daily_report',$data);
}
Model: Admin_Model
public function get_daily_report($daily_date)
{
$this->db->select('c.userid,c.dated_on,c.amount,c.trans_type,c.from_user,c.to_user,c.purpose,l.loginid,l.name');
$this->db->from('cash_transactions as c');
$this->db->join('login as l', 'c.userid = l.loginid');
$this->db->where('c.dated_on',$daily_date);
$query = $this->db->get();
$result =$query->result();
return $result;
}

Related

Search Query by Child field Eloquent relation

My Product Model is
public function IncomeRepo(){
return $this->hasMany(Income::class,'Product');
}
My Income Report Model Is
public function ProductData(){
return $this->belongsTo(Product::class,'Product','id');
}
My Query is
public function SearchIncomeData(Request $request){
$GetFromDate = $request->FromDate;
$GetToDate = $request->ToDate;
$ProductData = Product::with('IncomeRepo')->whereBetween('created_at', [$GetFromDate, $GetToDate])->get();
return view('Admin.Report.ProductSalesReport',compact('ProductData'));
}
When I return $ProductData it return products table created_at Data. BUT I nee Income table created_at data which be multiple
How can I get it?
My expectation show incomes table created_at data
If you want to filter data by child tables date then you need to use whereHas relationship.
$GetFromDate = $request->FromDate;
$GetToDate = $request->ToDate;
$ProductData = Product::with('IncomeRepo')
->whereHas('IncomeRepo',function($que) use($GetFromDate,$GetToDate) {
$que->whereBetween('created_at',[$GetFromDate, $GetToDate])})->get();
return view('Admin.Report.ProductSalesReport',compact('ProductData'));
In controller. (Use whereBetween)
public function SearchIncomeData(Request $request)
{
$GetFromDate = $request->FromDate;
$GetToDate = $request->ToDate;
$ProductData = Product::with(['IncomeRepo' => function ($query) use ($GetFromDate, $GetToDate)
{
$query->whereBetween('created_at', [$GetFromDate, $GetToDate]);
}
])->get();
return view('Admin.Report.ProductSalesReport', compact('ProductData'));
}
In view
foreach ($ProductData as $product)
{
foreach ($product->IncomeRepo as $income)
{
echo $income->created_at;
}
}

Multiple orWhere() in whereHas() in Laravel 5

Contact Model
class Contact extends Model
{
public function Account()
{
return $this->belongsTo('app\Account');
}
}
Account Model
class Account extends Model
{
public function Contact()
{
return $this->hasMany('app\Contact');
}
}
I want to get query of all Contacts that have account_name = 'test' or account_city ='test2'.
$query = Contact::query();
$query->whereHas('Account', function ($q) {
$q->orwhere('account_name' ,'=', 'test');
$q->orwhere('account_city','=','test2');
});
$query->get;
This query shows me all Contacts that have an Account but I want to get only Contacts that have account_name = 'test' or account_city ='test2'
Result of $query->tosql();
select * from `contacts` where exists (select * from `accounts` where (`contacts`.`account_id` = `accounts`.`id` or (`account_name` = ? or `account_city` = ?)) and `accounts`.`deleted_at` is null) and `contacts`.`deleted_at` is null
try this:
$query=Contact::query();
$query->whereHas('Account', function ($q) {
$q->where(function($query) {
$query->where('account_city','test2')->orWhere('account_name','test');
});
});
$query->get();
$query=Contact::query();
$query = $query->whereHas('Account', function ($q) {
$q->orwhere('account_name',test');
$q->orwhere('account_city','test2');
});
$query->get;
I think it will do the trick.

Codeigniter get database value in controller

This is my controller
function index($id=NULL)
{
$this->load->model('article');
$id= $this->uri->segment(3);
$data = array('article' => $this->article->show_a_article($id),
'sidebar' => $this->article->side_bar(9),
'related'=> $this->article->related_keyword($article_title),
);
echo $article->News_News_ID;
$this->load->view('page/baiviet',$data);
}
I want to get article_title value from table article and put it in related_workword() function.
Update model The show a article model is to get article detail. The second model is to get all article related to the chosen article.
function show_a_article($id)
{
$query= $this->db->get_where('news_news', array('News_News_ID'=>$id));
return $query->row();
}
function related_keyword($rkey)
{
$sql="SELECT `News_News_ID`,`News_News_Headline`,`News_News_thumb1` FROM `news_news` WHERE `news_tags` like '%$rkey%' ORDER BY `News_News_ID` desc LIMIT 10";
$query= $this->db->query($sql);
return $query->result();
}
Update: Igot it
echo $this->article->show_a_article($id)->News_News_Headline;
Call model method from controller
$article_title = $this->article->get_article_details($id)->article_title;
Add function in model to return data.
function get_article_details($id)
{
$query = 'select article_title from table where col = '.$this->db->escape($id);
return $this->db->query($query)->row();
}

Error Number: 1066 Not unique table/alias: 'core_user'

I have 3 tables in a database.
core_user
core_company
ct_company
i want to join 2 tables core_user and core_company where foriegn key is cmp_id column..
but it shows an error.
Error Number: 1066
Not unique table/alias: 'core_user'
SELECT * FROM (core_user, core_user) JOIN core_company ON core_company.cmp_id = core_user.cmp_id WHERE usr_email = 'fahad#gmail.com' AND usr_password = '123456' AND cmp_name = 'corpoleave'
Filename: F:/xampp/htdocs/corpoLeave/application/models/loginmodel.php
Line Number: 10
here is my model. please help. thanks
<?php class LoginModel extends CI_Model{
public function login_valid($email,$password,$cname){
$q= $this->db->where(['usr_email'=>$email,'usr_password'=>$password,'cmp_name'=>$cname])
->from('core_user');
$this->db->join('core_company', 'core_company.cmp_id = core_user.cmp_id');
$q = $this->db->get('core_user')->result();
if($q->result()==true)
{
return $q->row()->user_id;
}
else{
return false;
}
}}
?>
In the Codeigniter Query Builder, the get() and from() methods perform similarly.
As seen on the Codeigniter docs:
get()
$query = $this->db->get('mytable');
// Produces: SELECT * FROM mytable
from()
$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
// Produces: SELECT title, content, date FROM mytable
Both the get("tablename") and from("tablename") methods effectively append FROM tablename to the built statement, looking something like this:
$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get('mytable');
// Produces: SELECT title, content, date FROM mytable FROM mytable
(Notice the double "FROM" statement)
You are getting the error because you are using both from() and get() with tablenames. You can either remove the from() statement, or remove the tablename from the get() statement.
This would look like:
<?php
class LoginModel extends CI_Model {
public function login_valid($email, $password, $cname) {
$this->db->where(['usr_email' => $email, 'usr_password' => $password, 'cmp_name' => $cname]);
$this->db->from('core_user');
$this->db->join('core_company', 'core_company.cmp_id = core_user.cmp_id');
$q = $this->db->get();
if ($q->result()) {
return $q->row()->user_id;
} else {
return false;
}
}
}

dropdownlist not working in yii

the thing is i have 3 models wherein model1 model2's xyz(pid) is invoked which fetches the all id's contain that pid ,know model3 have (id,name) for the model2 fetched ids(fk of model3 id) ,i have looped them ,$data should store the name,id and a list of all id,name must appear in the dropdown of model1 view create
but i get the error stating
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause'. The SQL statement executed was:
SELECT id,name
FROM table1
WHERE id = Array
model1
public function myabcDropDown()
{
$pid=Yii::app()->SESSION['pid'];
$sid=model2::model()->xyz($pid);
foreach($sid as $val) //line 0
{
$data=model3::model()->pqr($val);
$data.=$data;
}
//line 1
$datalist=CHtml::listData($data,'id','name');
return $datalist;
}
function in model3
public function pqr($val)
{
$sql="SELECT id,name
FROM table1
WHERE id = ".$val;
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$tts=$command->queryRow();
$result=array();
foreach($tts as $key=>$val1)
{
$result[]=$val1;
}
return $result;
}
function in model2
public function xyz($pid);
{
$sql="SELECT id
FROM table3
WHERE id = ".$pid;
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$tts=$command->queryAll();
$result=array();
foreach($tts as $key=>$val)
{
$result[]=$val;
}
return $result;
}
Please help I am losing my mind on it
$result of xyz() is an array of rows(which in turn are arrays of fields) not of fields since queryAll was used. Use queryRow instead.
You need to learn about queryScalar() as they can help you a ;lot. now looking into your code you can do this
public function xyz($pid);
{
$sql="SELECT id
FROM table3
WHERE id = ".$pid;
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$result=$command->queryColumn();
return $result;
}
Then
public function myabcDropDown()
{
$data=array();
$pid=Yii::app()->SESSION['pid'];
$sid=model2::model()->xyz($pid);
if(!empty($sid))
{
foreach($sid as $value)
{
$data[]=model3::model()->pqr($value);
}
$datalist=CHtml::listData($data,'id','name');
return $datalist;
}
else
{
throw new CHttpException('I got no result');
}
}
and then finally
public function pqr($val)
{
$criteria=new CDbCriteria;
$criteria->select='id,name';
$criteria->condition='id=:id';
$criteria->params=array(':id'=>$val);
$result=Table1::model->find($criteria);
return $result;
}
Information:-
queryScalar() = You should use it if you are sure that your query is going to return single result only. It will make you free from the headache of arrays.
queryColumn() = You should use it if your are going to select a single column values in your query. It will return you one dimensional array and hence making you free from the headache of multidimensional arrays.
This wrks
public function myabcDropDown()
{
$pid=Yii::app()->SESSION['pid'];
$sid=model2::model()->xyz($pid);
$datalist=CHtml::listData($sid,'id','name');
return $datalist;
}
public function xyz($pid);
{
$sql="select id ,name from table1 where id IN (SELECT id
FROM table2
WHERE pid = ".$pid.")";
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$tts=$command->queryAll();
$result=array();
foreach($tts as $key=>$val)
{
$result[]=$val;
}
return $result;
}
and no need od pqr() any more

Resources