0

I tried with google and StackOverflow but there is nothing useful.

Does anyone know how to extend Query Builder? For example, I want to write my own count() method from Query Builder...

/**
 * Retrieve the "count" result of the query.
 *
 * @param  string  $columns
 * @return int
 */
public function count($columns = '*')
{
    if (! is_array($columns)) {
        $columns = [$columns];
    }
    return (int) $this->aggregate(__FUNCTION__, $columns);
}
fico7489
  • 7,931
  • 7
  • 55
  • 89
  • 1
    a well explained answer https://stackoverflow.com/questions/24555025/how-to-customize-laravels-database-query-builder-make-better-subquery/24561500 – Sohel0415 Jan 22 '18 at 19:22
  • Technically what you're describing is overriding and not extending. If you were to extend, the query builder uses the Macroable trait so you can add macros to it with your own custom functions, but those can't replace the built-in ones. – apokryfos Jan 22 '18 at 20:01
  • @apokryfos you are just playing with words I don't see anything useful with your constatation. So you are telling me that this is not extending "class MyBuilder extends Builder" and then my implementation of count() in MyBuilder? – fico7489 Jan 22 '18 at 20:04
  • You're extending the query builder but you are trying to override framework functionality. The framework doesn't have a straight forward way to allow that and that's probably because it's not meant to support that. This is just a word of caution. – apokryfos Jan 22 '18 at 20:06
  • @apokryfos you are right for this, but problem is that count() is not working well when I use select() and groupBy()... – fico7489 Jan 22 '18 at 20:08

0 Answers0