@@ -94,6 +94,11 @@ abstract class Relation extends Field implements Form
9494 */
9595 protected ?Closure $ aggregateResolver = null ;
9696
97+ /**
98+ * Determine whether the relation values are aggregated.
99+ */
100+ protected bool $ aggregated = false ;
101+
97102 /**
98103 * The option group resolver.
99104 */
@@ -380,15 +385,15 @@ public function resolveDisplay(Model $related): ?string
380385 $ this ->display ($ related ->getKeyName ());
381386 }
382387
383- return call_user_func_array ($ this ->displayResolver , [$ related ]);
388+ return ( string ) call_user_func_array ($ this ->displayResolver , [$ related ]);
384389 }
385390
386391 /**
387392 * {@inheritdoc}
388393 */
389394 public function getValue (Model $ model ): mixed
390395 {
391- if (is_callable ( $ this ->aggregateResolver ) ) {
396+ if ($ this ->aggregated ) {
392397 return parent ::getValue ($ model );
393398 }
394399
@@ -410,7 +415,7 @@ public function resolveFormat(Request $request, Model $model): ?string
410415 $ this ->formatResolver = function (Request $ request , Model $ model ): mixed {
411416 $ default = $ this ->getValue ($ model );
412417
413- if (is_callable ( $ this ->aggregateResolver ) ) {
418+ if ($ this ->aggregated ) {
414419 return (string ) $ default ;
415420 }
416421
@@ -541,6 +546,8 @@ public function aggregate(string $fn = 'count', string $column = '*'): static
541546 $ column === '* ' ? '' : sprintf ('_%s ' , $ column )
542547 ));
543548
549+ $ this ->aggregated = true ;
550+
544551 return $ query ->withAggregate ($ this ->getRelationName (), $ column , $ fn );
545552 };
546553
0 commit comments