Correlate subquery to joined query - doctrine

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?

Related

Laravel : Get records from a table with 'where clause' from a field in another table

i have two tables . first is 'projects' with field id,name,number and second table is 'happenings' with fields id,project_id . these tables have one-to-many relationship . How can i get records from 'happenings' where their 'number' field in 'projects' is for example 5 .
Try something like :
SQL query :
SELECT *, round(AVG(h.progress),0) as Progress FROM happenings h
JOIN projects p on p.id = h.project_id
WHERE p.number = 5
Eloquent :
$data = DB::table('happenings as h')
->join('projects as p','p.id', '=', 'h.project_id')
->select('*', DB::raw('round(AVG(h.progress),0) as Progress'))
->where('p.number','=',5)
->get();

Unknown column total when using where

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 . '%');

Laravel Eloquent: how to use “where” with WithCount to select Models whose count is larger than a number

Suppose I have two tables: posts and tags. I want to eager load all the tags with posts whose post count is larger than 10. How would I do it?
I tried the following but not work:
Tag::withCount('posts')
->where('posts_count', '>' , 10)
->get();
It gives me the following error:
Column not found: 1054 Unknown column 'posts_count' in 'where clause' (SQL: select `tags`.*, (select count(*) from `posts` inner join `post_tag` on `posts`.`id` = `post_tag`.`post_id` where `tags`.`id` = `post_tag`.`tag_id`) as `posts_count` from `tags` where `posts_count` > 1 order by `posts_count` desc)
try this:
Tag::withCount('posts')->having('posts_count', '>' , 10)->get();
If you want to filter on aggregates, you need to use having.

Laravel query using group by and where does not work

This is a query that I have in raw sql.
DB::select('SELECT bldgs.name as building , floors.name as floor, areas.name as area, locations.area_id ,count(reqs.location_id) as occupied FROM `reqs` '
. 'JOIN locations ON locations.id = location_id '
. 'JOIN areas ON areas.id = area_id '
. 'JOIN floors ON floors.id = areas.floor_id'
. ' JOIN bldgs ON bldgs.id = bldg_id '
. 'WHERE `status`=2 and (DATE_FORMAT(start_date,"%Y-%m")<= "'.$dateFrom.'" AND DATE_FORMAT(end_date,"%Y-%m")>="'.$dateTo.'") group by locations.area_id, areas.name, floors.name, bldgs.name' );
And this is one of many attempts to make it work in Laravel elequent instead of raw.
Req::select('bldgs.name as building',DB::raw('count(location_id) as count_occupied'))
->join('locations','locations.id','=','location_id')
->join('areas','areas.id','=','locations.area_id')
->join('floors','floors.id','=','areas.floor_id')
->join('bldgs as bl','bl.id','=','floors.bldg_id')
->where('reqs.status','=', '2')
->where('start_date','<=', $date)
->where('end_date','>=', $date)
->groupBy('bldgs.name')
I need to understand why the second way gives mysql error and refuses to run the query above. Is is a mistake in my code or is this normally not possible in using eloguent to group by like this except in raw mysql string?
This is the error I get.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bldgs.name' in 'field list' (SQL: select `bldgs`.`name` as `building`, count(location_id) as count_occupied from `reqs` inner join `locations` on `locations`.`id` = `location_id` inner join `areas` on `areas`.`id` = `locations`.`area_id` inner join `floors` on `floors`.`id` = `areas`.`floor_id` inner join `bldgs` as `bl` on `bl`.`id` = `floors`.`bldg_id` where `reqs`.`status` = 2 and `start_date` <= 2018-10 and `end_date` >= 2018-10 group by `bldgs`.`name`)
As the error states, you don't have a bldgs.name column. You named the bldgs table bl when you joined it.
Rename your references from bldgs.name to bl.name.

How to set a value in join() rather a column name in Eloquent?

Here is my schemes:
user_familiarity
id, item_type, item_id, score, custom_score , ...
word
id , ...
user_familiarity stores familiarity score of any object, which can identified by (item_type, item_id)
for example:
item_type = 1001, item_id = 16 means table word and row's id is 16
Here is my code to select words with it's familiarity score:
$fam_table = UserFamiliarityModel::getTableName();
return
$query
->leftJoin($fam_table, function ($join) use($fam_table) {
$join
->on(WordModel::getTableName() . '.id' ,'=', "${fam_table}.item_id")
->on($fam_table . '.item_type' , 1001);
})
->addSelect(["${fam_table}.score", "${fam_table}.custom_score"]);
here is the sql that has been executed:
...
FROM
`word`
LEFT JOIN `user_familiarity`
ON
`word`.`id` = `user_familiarity`.`item_id`
AND `user_familiarity`.`item_type` = `1001`
and get errors :
Column not found: 1054 Unknown column '1001'
... I solve this problem by changing 'on' to 'where' :
->leftJoin($fam_table, function ($join) use($fam_table) {
$join
->on(WordModel::getTableName() . '.id' ,'=', "${fam_table}.item_id")
->where($fam_table . '.item_type' , 1001);
})
change on to where
because you have specific value

Resources