I have made this query:
$query = UserHistoryAccess::with('user')
->select('user_history_accesses.*', DB::raw('count(user_history_accesses.id) as total'))
->join('users', 'user_history_accesses.user_id', 'users.id')
->groupBy('user_history_accesses.user_id')
->where('total', 'like', '%' . $term . '%')
;
Even I have followed this solution but I got this error :
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total' in 'where clause' (SQL: select count(*) as aggregate from user_history_accesses inner join users on user_history_accesses.user_id = users.id where total like %388%) in file C:\xampp\htdocs\HabilitisBack\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 671
you can not use result colum in the where, you should recalculate it in the where to get the desired result, but try using 'having':
->having('total', 'like', '%' . $term . '%');
Related
I am trying to set up the following SQL in Laravel.
$projects = DB::table(DB::raw('places, areas, areas_places'))
->select('projects.*')->distinct()
->leftJoin('tags_places as r', 'r.place_id', '=', 'places.place_id')
->leftjoin('tags as t', 't.id', '=', 'r.tag_id')
->join('areas_places as e', 'e.place_id', '=', 'places.place_id')
->join('areas as a', 'a.area_id', '=', 'e.area_id')
However, I keep getting the following error.
Column not found: 1054 Unknown column 'places.place_id' in 'on clause'
The column exists in the database. I've also checked to see if the column is spelled correctly-- it is. So what could I be missing? I am using Laravel Mix version 4.0.7
I figured out what the issue was. I just didn't need the DB::raw statement. This works as expected without problems:
$projects = DB::table('projects')
->select('projects.*')->distinct()
->leftJoin('tags_places as r', 'r.place_id', '=', 'places.place_id')
->leftjoin('tags as t', 't.id', '=', 'r.tag_id')
->join('areas_places as e', 'e.place_id', '=', 'places.place_id')
->join('areas as a', 'a.area_id', '=', 'e.area_id')
IGP's suggestion to check with toSql() was helpful in figuring this out.
I currently have 2 columns in my table going by the name of uploadDate and uploadTime. I have a clause like this:
->orWhere('uploadDate', 'like', '%' . $request->search . '%')
->orWhere('uploadTime', 'like', '%' . $request->search . '%');
The problem is that if $request->search = yyyy-mm-dd, hh:mm:ss, my web app is not able to get any results since uploadDate and uploadTime are seperate columns on the DB. Does anyone have a way of creating a orWhere statement that concatinates both uploadDate and uploadTime?
I know this can be easily achieved by just creating a single column in the DB that merges both columns but I'm just looking for an easier way out at this point, hahaha.
You shouldn't use like or orWhere() here if you want to find records by exact date and time.
->where('uploadDate', str_before($request->search, ','))
->where('uploadTime', str_after($request->search, ', '))
If you have other where() or orWhere() clauses in the query, you can do this:
->where([
'uploadDate', '=', str_before($request->search, ','),
'uploadTime', '=', str_after($request->search, ', ')
])
I have the following query:
$this->data = \DB::table('months')->select(DB::raw("months.id, COUNT(transactions.id) as total"))
->leftJoin('transactions', function($join)
{
$join->on('months.id', '=', DB::raw('MONTH(created_at)'))
->on('transactions.doctor_id', '=', $this->user_id);
})
->groupBy('months.id')
->get();
It involks an error on line ->on('transactions.doctor_id', '=', $this->user_id);. It added single quotes for variable $this->user_id.
How to avoid this eroros:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in
'on clause' (SQL: select months.id, COUNT(clients.id) as total
from `months` left join `clients` on `months`.`id` = MONTH(created_at)
and `clients`.`doctor_id` = `2` group by `months`.`id`)
You can try to use DB:raw like this:
->on('transactions.doctor_id', '=', DB::raw($this->user_id));
I tried to use a join, with an array given as a condition:
$task = Task::join('oc_groups', function($join) use ($filter) {
foreach($filter['groups']['data'] as $key => $value) {
$join->on('oc_groups.id', $value);
}
});
But I get the error message :
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on clause' (SQL: select oc_tasks.title as task_title from oc_tasks inner join oc_groups on oc_groups.id = 1 where oc_tasks.task_date between 2017-07-01 and 2017-07-31)
The 1 is the content of the $value. What I am doing wrong? - The table oc_groups has a field named id.
As you don't have any relation between these two tables so Try with out join. like this
select `oc_tasks`.`title` as `task_title` from `oc_tasks` ,`oc_groups`
where `oc_tasks`.`task_date` between 2017-07-01 and 2017-07-31 and `oc_groups`.`id` = `1`
I have this filter addition
$subQuery = $query->createSubquery()
->select ('ps.id')
->from('Person ps')
->innerJoin('ps.JoomlaUser ju')
->innerJoin('ju.OrderList o')
->innerJoin('o.ItemList oi')
->whereIn ('oi.product_id',$value);
$subSubQuery = $subQuery->createSubquery()
->select('oh.order_id')
->from ('jos_vm_order_history oh')
->addWhere ('oh.order_status_code=?','C')
->addWhere ('oh.order_id=o.order_id') //<-- this doesn't work
->orderBy ('oh.date_added desc')
->limit(1);
$subQuery->addWhere('exists (' . $subSubQuery->getDql() . ')');
$query->addWhere('person_id in (' . $subQuery->getDql() . ')');
in which I need to correlate a sub query to table in a joined query. When I execute this I get
Column not found: 1054 Unknown column 'o.order_id' in 'where clause'
How can I get the query alias of the "OrderList" relation that is used in the final query?