Skip to content

Commit

Permalink
Fix Collection::implode() string argument (#41468)
Browse files Browse the repository at this point in the history
collect(['foo', 'bar'])->implode('_') should be 'foo-bar' but it
instead attempts to call translator helper _().

For callable only support Closure, an invokable class, or a
[class, 'method'] array pair.
  • Loading branch information
derekmd committed Mar 14, 2022
1 parent bd422ae commit 9e7d31d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ public function hasAny($key)
*/
public function implode($value, $glue = null)
{
if (is_callable($value)) {
if ($this->useAsCallable($value)) {
return implode($glue ?? '', $this->map($value)->all());
}

Expand Down
1 change: 1 addition & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2151,6 +2151,7 @@ public function testImplode($collection)
$data = new $collection([Str::of('taylor'), Str::of('dayle')]);
$this->assertSame('taylordayle', $data->implode(''));
$this->assertSame('taylor,dayle', $data->implode(','));
$this->assertSame('taylor_dayle', $data->implode('_'));

$data = new $collection([['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']]);
$this->assertSame('taylor-foodayle-bar', $data->implode(fn ($user) => $user['name'].'-'.$user['email']));
Expand Down

0 comments on commit 9e7d31d

Please sign in to comment.