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.