Category: Laravel, PHP

So let’s talk in a high-level language, “an asset has many calibrations and the valid calibration is only the most recent one”. If one wants to do a select query for every asset that has a good calibration status where that constraint is present, that person would need to select based on the latest row of calibrations data. Laravel has provided its users with a method to do such a specifics job and that is whereHas.

The query would return the asset with the id of 1 although the last calibration is done with a status of zero and render the query to be invalid.

The solution is to use the query builder join while only selecting the fields from the assets table and combining it with a subquery to match the calibration id with the latest calibration id that corresponds to the asset in the relation.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners