I'm trying to run a method on each element inside a collection. It's an object method residing in the same class:
protected function doSomething() { $discoveries = $this->findSomething(); $discoveries->each([$this, 'doSomethingElse']); } protected function doSomethingElse($element) { $element->bar(); // And some more }
If I precede the call on Collection::each
with the check is_callable([$this, 'doSomethingElse'])
it returns true, so apparently it is callable. The call itself however throws an exception:
Type error: Argument 1 passed to Illuminate\Support\Collection::each() must be callable, array given, called in ---.php on line 46
The method trying to be called can be found here.
I'm bypassing this by just passing a closure that itself simply calls that function, but this would definitely a much cleaner solution and I can't find out why it throws the error.
Change the visibility of your callback method to public.
protected function doSomething() { $discoveries = $this->findSomething(); $discoveries->each([$this, 'doSomethingElse']); } public function doSomethingElse($element) { $element->bar(); // And some more }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With