0
votes

i am trying to group tags by first character

return $tags->select('id', 'title')
    ->orderBy('title','asc')
    ->groupBy(function($item, $key) { 
        return substr($item['title'], 1, 1); 
    })
    ->get();

but it returns this error

strtolower() expects parameter 1 to be string, object given

1
Is the error from a Laravel file, or by you own code? - AnthonyB
Can you provide the code where you call strtolower? - Alex Harris
i think its error is from this part groupBy(function($item, $key) { return substr($item['title'], 1, 1); }) - lekinio
i am not calling strtolower - lekinio
PHP errors usually have file & line specified. It'd be useful if you specified the full error here. Other suggestion would be using xdebug which also allows you to have backtraces displayed on errors. Then you'd know where the error is coming from. - StasM

1 Answers

6
votes

You can't group the query using a closure, only the resulting collection:

...->orderBy(...)->get()->groupBy(...);