Max and min values from related table - cakephp 4

I have a table of housing developments that has a simple structure -

development_id, name, address, etc

Each development has a number of plots for sale with a structure like -

plot_id, development_id, price

They are related in the DevelopmentsTable.php file -

        $this->hasMany('Plots', [
            'foreignKey' => 'development_id',
        ]);

For each development I need to show what the maximum and minimum prices for the plots the development contains are. I’m not at all sure how to go about doing this except by cycling through all the plots in the view and finding the maximum and minimum which doesn’t seem like a good way of doing it.

Any suggestions on a better way would be useful.

Use the min() and max() aggregate functions: see Using SQL Functions here:

https://book.cakephp.org/4/en/orm/query-builder.html

1 Like

Thanks @jimgwhit . Someone in another forum suggested using the min and max functions in collections which is probably closer to what I was after - Collections - 4.x