#Query Where Has Nested Relationship but grabbing the latest entry of that relation only
6 messages · Page 1 of 1 (latest)
the name isn't stored on the status what do you want me to query by?
the model_id would be no ?
So if I swap it to this: https://cdn.dectom.dev/mp4C9J7VoY.png
Then it returns the one that has the type of Provisioned https://cdn.dectom.dev/SmNoMlGZic.png
my problem is that, The device with type of Provisioned has had a previous status of In Stock, but I only care about the current status, not it's historical status
but this model has had a status previously of In Stock, which is why the whereHas picks it up
Because when it goes to sql it just does this:
SELECT
*
FROM
`devices`
WHERE
EXISTS( SELECT
*
FROM
`statuses`
WHERE
`devices`.`id` = `statuses`.`statusable_id`
AND `statuses`.`statusable` = 'App\\Models\\Devices\\Device'
AND EXISTS( SELECT
*
FROM
`status_types`
WHERE
`statuses`.`status_type_id` = `status_types`.`id`
AND `name` = 'In Stock'
AND `status_types`.`deleted_at` IS NULL)
AND `statuses`.`deleted_at` IS NULL)
AND `devices`.`deleted_at` IS NULL
Which isn't taking into account the whole it should only get the most recent where checking the status type relation on the status