Problem
I am trying to make a scope in my model with this SQL query:
SELECT *
FROM ro_container_events
WHERE (container_id, location_timestamp, id)
IN (
SELECT distinct container_id, MAX(location_timestamp) AS lts, MAX(id) AS rce_id
FROM ro_container_events
GROUP BY container_id
)
The model name is ContainerEvent and the name of the table in my database is ro_container_events.
Also, I know that the SQL query is valid because I ran it in my MySQL administration tool (HeidiSQL), and it returns the good rows.
What I've tried
My scope (in my ContainerEvent model) currently looks like this:
public function scopeLatestEventForContainers($query)
{
return $query->select(DB::raw('
SELECT *
FROM ro_container_events
WHERE (container_id, location_timestamp, id)
IN (
SELECT distinct container_id, MAX(location_timestamp) AS lts, MAX(id) AS rce_id
FROM ro_container_events
GROUP BY container_id
)'
)
);
}
But it doesn't return any rows?
My research
I've been searching about this topic for a while but can't seem to find what is wrong with my scope...
The Laravel documentation says that we can use :
DB::raw('...')
In order to make a specific SQL query.
And I've seen on some other threads that I should be able to make a scope with the following :
return $query->select(DB::raw('...');