Return Only The Highest Equipment Maintenance Id
I want to get the highest
equipament to show to a user. I can do this in SQL, but not in Eloquent or QueryBuilder.
Eloquent returns all the maintences for my view:
$equipament = Maintence::SELECT('maintences.*', 'equipaments.patrimony as patrimony') ->LEFTJOIN('equipaments', 'maintences.equipament_id', '=', 'equipaments.id' ) ->get();
SQL returning the highest
select * from equipaments left join maintences on maintences.equipament_id = equipaments.id and maintences.id = (select max(id) from maintences as main where main.equipament_id = equipaments.id )
I think this is what you're looking after:
Equipment::select('equipments.id', 'equipments.name', 'maintenances.id as maintenance_id') ->leftJoin( \DB::raw( '(select max(id) as id,equipment_id as equipment_id from maintenances group by equipment_id order by id desc) maintenances' ), 'maintenances.equipment_id', '=', 'equipments.id') ->get();
I assume your table names are
maintenances, if not, please adjust the table names accordingly within the provided query.
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?