I am working on a laravel project(Laravel 6.8). I have a locations table and a dashboards table. I am trying to build a query that will return all matching records from the dashboards table. Looking at another SO question(Laravel 5.4 Raw Join Query), I saw a similar need and tried to adjust the code to fit my needs, but it still doesn't work.
This is what I have right now.
$locations = DB::table('locations')
->selectRaw("site_name,
COUNT(DISTINCT client) as client_count,
GROUP_CONCAT(client_lob) as lobs,
COUNT(DISTINCT client_lob) as lob_count,
SUM(agent_workstations) as aw_sum,
SUM(production_support_workstations) as psw_sum,
locations.*" )
->leftJoin('dashboards', 'locations.id', '=', 'dashboards.location_id')
->whereNotNull('latitude')
->groupBy(['site_name'])
->orderBy('site_name', 'asc')
->get();
It returns only the data from locations without returning anything from dashboards. I can't see what is wrong with this query. Can anyone offer some advice?
Because you never select the dashboard's fields, try to select the dashboard's field like this:
$locations = DB::table('locations')
->selectRaw("site_name,
COUNT(DISTINCT client) as client_count,
GROUP_CONCAT(client_lob) as lobs,
COUNT(DISTINCT client_lob) as lob_count,
SUM(agent_workstations) as aw_sum,
SUM(production_support_workstations) as psw_sum,
locations.*, dashboards.column1, dashboards.column2" )
Related
Background
I have written a SQL query that works perfectly for my needs, and I am trying to add it to a queued job in Laravel. Instead of using Eloquent models I wanted to go with DB Query to ensure better performance as there will ultimately be many joins and conditions. I am having a problem with the where clause when checking a date, but it works perfectly fine when running in SQL. I have stripped out additional pieces of this query to only include what is necessary for debugging this issue.
Code
The original SQL:
SELECT `messages`.`created_at`, `participants`.`last_read`
FROM `messages`
LEFT JOIN `participants` ON `messages`.`thread_id` = `participants`.`thread_id` AND `messages`.`user_id` != `participants`.`user_id`
WHERE `messages`.`created_at` > `participants`.`last_read`;
When I run this directly in SQL, I see 2 results, which is expected with my current data.
created_at
last_read
2021-03-26 19:02:53
2021-03-23 19:31:30
2021-03-26 19:02:58
2021-03-23 19:31:30
This is how I have written it in Laravel:
$query = DB::table('messages')
->leftJoin('participants', function ($join) {
$join->on('messages.thread_id', '=', 'participants.thread_id')
->on('messages.user_id', '!=', 'participants.user_id');
})
->select('messages.created_at as message_date', 'participants.last_read as last_read')
->where('messages.created_at', '>', 'participants.last_read');
When I execute this, the results are empty.
I dumped the final SQL from the DB Query builder to make sure it's correct, and this is what it is:
select `messages`.`created_at` as `message_date`, `participants`.`last_read` as `last_read`
from `messages`
left join `participants`
on `messages`.`thread_id` = `participants`.`thread_id` and `messages`.`user_id` != `participants`.`user_id`
where `messages`.`created_at` > participants.last_read
And running that directly in SQL returns accurate results, as expected.
Context
For context, here is the data structure and some of the data I'm working with.
participants
id
thread_id
user_id
last_read
created_at
updated_at
deleted_at
last_notified
9
8
178
2021-03-23 23:31:53
2021-03-23 22:16:48
2021-03-23 23:31:53
NULL
NULL
messages
id
thread_id
user_id
body
created_at
updated_at
deleted_at
159
3
177
adfad
2021-03-26 19:02:53
2021-03-26 19:02:53
NULL
160
3
177
dadddda
2021-03-26 19:02:58
2021-03-26 19:02:58
NULL
The problem
It seems as though the DB query code is causing the columns with like names to be mixed up. Both tables have a column called created_at, but I only need that column from the messages table. My SELECT only asks for that column, specifying the correct table. But something in this DB Query join is causing it to get mixed up.
Playing with different joins, and removing the where clause, I realized that the dates aren't correct always. For example, here is the result when I use leftJoin
{
"message_date": "2021-03-23 00:30:42",
"last_read": "2021-03-26 00:22:48"
},
{
"message_date": "2021-03-23 00:31:25",
"last_read": "2021-03-26 00:22:48"
}
Notice, the message_date and last_read values are reverse of what they were when running the SQL directly. So this must be the problem.
I changed to rightJoin, and the results are reversed:
{
"message_date": "2021-03-26 19:02:53",
"last_read": "2021-03-23 19:31:30",
},
{
"message_date": "2021-03-26 19:02:58",
"last_read": "2021-03-23 19:31:30",
}
So that should work, right? I add the where clause back in, but still the results are empty.
I am guessing there is something I need to do to tell the query builder to handle these columns correctly, as they seem to be getting mixed up during the where and select. But I can't figure out how to clarify that. I have tried searching for others with this issue but I can't seem to find anything relevant.
Already tried
I have already tried a few things with no change in results.
Changing the order of the commands - like moving the select() to the beginning of the statement, things like this.
Using whereDate instead of where. (Note - for performance I'd rather avoid this, but wanted to try just in case).
Using join, joinLeft, and joinRight.
Using where in the on clause instead of two ons. Like this
->leftJoin('participants', function ($join) {
$join->on('messages.thread_id', '=', 'participants.thread_id')
->where('messages.user_id', '!=', 'participants.user_id');
})
Anyone have any guidance on things I can try? This should be such a simple task, and has turned into hours of trying to understand why it works in SQL and not Laravel's DB Query Builder.
The where function of the query builder will always assume the right hand side is a value and will use it in a prepared statement as a literal (in this case string). If you want to compare columns you need to use whereColumn:
$query = DB::table('messages')
->leftJoin('participants', function ($join) {
$join->on('messages.thread_id', '=', 'participants.thread_id')
->on('messages.user_id', '!=', 'participants.user_id');
})
->select('messages.created_at as message_date', 'participants.last_read as last_read')
->whereColumn('messages.created_at', '>', 'participants.last_read')->get();
Additional where clauses can be found in the docs
I have two tables user_master and user_teams with common field user_name.
I want to join the table and get the team value group by teams
tried as
$filter_teams = DB::table('user_teams')
->join('user_master','user_master.user_name','=','user_teams.user_name')
->whereIn('user_master.geo',$geo)
->groupBy('user_teams.team')
->pluck('user_teams.team')
->toArray();
by may values are duplicating.I'm using postgresql
since you didn't determine select fields ... the default will be '*'
this is why you are getting duplicated fields ...
just add :
->select('user_teams.team')
and i think that all.
i recommend not using group without aggregation ....
so my advice your query to like this:
$filter_teams = DB::table('user_teams')
->join('user_master','user_master.user_name','=','user_teams.user_name')
->whereIn('user_master.geo',$geo)
->select('user_teams.team')->distinct()
->pluck('user_teams.team')
->toArray();
I am working on a project using the Laravel framework. In this project I have three tables:
1) Master Part Numbers (master_part_numbers)
Columns: id, part_number
Values: 1, MS26778-042
2) Inventory (inventory)
Columns: id, master_part_number, stock_qty
Values: 1, 1, 7
3) Inventory Min Maxes (inventory_min_maxes)
Columns: id, master_part_number, min_qty
Values: 1, 1, 10
I am trying to find the inventory where the stock level is below the min_qty. I have been attempting this using joins, like so:
$test = MasterPartNumber::table('master_part_numbers')
->join('inventory', 'master_part_numbers.id', '=', 'inventory.master_part_number_id')
->join('inventory_min_maxes', 'master_part_numbers.id', '=', 'inventory_min_maxes.master_part_number_id')
->select('master_part_numbers.part_number')
->where('inventory.stock_qty', '<=', 'inventory_min_maxes.min_qty')
->get();
However I am getting an empty collection every time. I have tried removing the where() clause and I get all the part numbers in the inventory, so it feels like I'm on the right track, but missing a critical component.
Also, I don't know if there is an easier or more efficient way to do this using Laravel's Eloquent Relationships, but that option is available.
Note: I added the space after table('master_part_numbers') in my query displayed here on purpose, for readability.
EDIT 1:
This sql query returns the expect result:
SELECT master_part_numbers.part_number
FROM master_part_numbers
JOIN inventory ON master_part_numbers.id=inventory.master_part_number_id
JOIN inventory_min_maxes ON master_part_numbers.id=inventory_min_maxes.master_part_number_id
WHERE inventory.stock_qty<=inventory_min_maxes.min_qty;
EDIT 2:
I finally got it working with some help from the Laravel IRC, however it isn't ideal because I am missing out on some of the data I would like to display, normally collected through relationships.
Here is what I am currently using, but I hope to get refactored:
DB::select(DB::raw('SELECT master_part_numbers.id, master_part_numbers.part_number, master_part_numbers.description, inventory.stock_qty, inventory.base_location_id, inventory_min_maxes.min_qty, inventory_min_maxes.max_qty
FROM master_part_numbers
JOIN inventory ON master_part_numbers.id = inventory.master_part_number_id
JOIN inventory_min_maxes ON master_part_numbers.id = inventory_min_maxes.master_part_number_id
WHERE inventory.stock_qty <= inventory_min_maxes.min_qty'));
If I have understood your problem correctly then
'masters_part_numbers.id' == 'inventory.id' and
'inventory.master_part_number' == 'inventory_min_maxes.master_part_number'
$test = DB::table('master_part_numbers')
->join('inventory', 'master_part_numbers.id', '=', 'inventory.id')
->join('inventory_min_maxes', 'inventory.master_part_number', '=', 'inventory_min_maxes.master_part_number')
->where('inventory.stock_qty', '<=', 'inventory_min_maxes.min_qty')
->whereNotNull('inventory_min_maxes.master_part_number');
->select(DB::raw('part_number'))
->get();
Based on above criteria. This code will work. I tried in laravel 5.4 .
Try and let me know. nd if it work give me a thumbs up
I discovered a way to solve this problem using the Laravel ->whereRAW() statement:
$test = $inventory->join('inventory_min_maxes', 'inventory.id', '=', 'inventory.inventory_min_max_id')
->whereRaw('inventory.stock_qty <= inventory_min_maxes.min_qty')
->whereRaw('inventory.inventory_min_max_id = inventory_min_maxes.id') // required so it tests against the specific record, without it will test against all records.
->get();
The major advantage for me, other than it looked terribly ugly before, was that I can now use the power of relationships.
Note: $inventory is an instance of my Inventory model, which I type hinted in the index() method.
I have the following query that I need to get working:
$repository = $this->getDoctrine()->getRepository('AppBundle:ProjectPhaseUser');
$query = $repository->createQueryBuilder('p')
->select('(p2.project) AS projectId')
->leftJoin('AppBundle:ProjectPhase', 'p2', 'WITH', 'p.projectPhase = p2.id')
->where('p.user = ' . $user->getId())
->orderBy('p.assignDate', 'DESC')
->setMaxResults(3)
->getQuery();
Basically there are 2 tables: ProjectPhaseUser and ProjectPhase
What resides in the ProjectPhaseUser table are the users with projectphases and an assigndate. In the ProjectPhase table you have the projectphases and projects they belong to.
What I want is to get the 3 last assigned projects of a particular user.
What the query does now is get the last 3 projectphases their projects. However this gives me duplicate projects since a project can have multiple projectphases. When I try a distinct on the project I get the problem that orderby is not in the select clause. I also have tried groupby which gives me the only full group by mysql error.
How can I achieve this without altering the mysql only full group by option?
I have this strange situation and not sure of what is wrong. I have a simple self join to find matches based on some conditions. I have this query running fine in mysql but when I call it through PHP, it doesn't return any values.
select * from Requests p inner join Requests c on c.ID<>p.ID
where usr_ID<>4
and p.c_ID = c.c_ID
This works fine but not the below one.
DB::table('Requests as parent')
->join('Requests as child', 'parent.ID', '<>', 'child.ID')
->where('parent.usr_ID', '<>', 4)
**->where('parent.c_ID', '=', 'child.c_ID')**
->get();
In the above query, if I remove the second where condition(c_ID), it returns correct values. For all rows, this has a value of 1. If I replace child.c_ID or parent.c_ID by 1, it works again. I have tried with other columns as well and found the same issue.
Any pointers?
What the query builder makes out of your second where condition is:
WHERE parent.c_ID = 'child.c_ID'
So instead of a "normal" where() use whereRaw(), which takes your input and injects it right into the final SQL query
->whereRaw('parent.c_ID = child.c_ID')
Alternatively you could also use DB::raw() on the third argument
->where('parent.c_ID', '=', DB::raw('child.c_ID'))
Both are essentially the same so use whichever you like more.