#Query Where Has Nested Relationship but grabbing the latest entry of that relation only

6 messages · Page 1 of 1 (latest)

distant geyser
#

ok lets get rid of type see if that is causing the issue

ancient osprey
#

the name isn't stored on the status what do you want me to query by?

distant geyser
#

the model_id would be no ?

ancient osprey
#

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