Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

doorcollider's avatar

eloquent join and groupBy

Hello, my join with tbl_statusReceptie has multiple rows with same tbl_statusReceptie.idFluxReceptie and i want to select the highest tbl_statusReceptie.statusZona value from the tbl_statusReceptie, how can i group them to get unique tbl_receptieProduse.idReceptie but with the highest value from statusReceptie.

$produse = ProduseReceptie::where('tbl_receptieProduse.idFluxReceptie', '=', $idFlux)

        ->join('tbl_produse', 'tbl_produse.idProdusErp', '=', 'tbl_receptieProduse.idProdus')
        ->join('tbl_statusReceptie', 'tbl_statusReceptie.idFluxReceptie', '=', 'tbl_receptieProduse.idFluxReceptie')


        ->orderBy('tbl_statusReceptie.statusZona', 'DESC')
        
        ->select(
            'tbl_receptieProduse.idReceptie',
            'tbl_receptieProduse.idFluxReceptie',
            'tbl_produse.numeProdusErp',
            'tbl_produse.codProdusErp',
            'tbl_produse.sn',
            'tbl_receptieProduse.dataExpirare',
            'tbl_receptieProduse.lot',
            'tbl_receptieProduse.cantitate',
            'tbl_statusReceptie.statusZona'
        )



        ->get();
0 likes
1 reply
Tray2's avatar

I'm sorry but who the hell built that database model? There are so many issues with it that you need to compensate for rather than being able to utilize the things that Laravel gives you for free.

I suggest you give this one a read.

https://tray2.se/posts/database-design

Please or to participate in this conversation.