Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayAccess;
use ArrayIterator;
use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\EnumeratesValues;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
Expand Down Expand Up @@ -627,8 +628,13 @@ public function implode($value, $glue = null)
}

$first = $this->first();
$pluck = is_array($first) || is_object($first);

if (is_array($first) || (is_object($first) && ! $first instanceof Stringable)) {
if ($first instanceof \Stringable && ! $first instanceof Model) {
$pluck = false;
}

if ($pluck) {
return implode($glue ?? '', $this->pluck($value)->all());
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
use Symfony\Component\String\ByteString;
use Symfony\Component\VarDumper\VarDumper;
use Traversable;
use UnexpectedValueException;
Expand Down Expand Up @@ -2332,6 +2333,13 @@ public function testImplode($collection)
$this->assertSame('taylor-foo,dayle-bar', $data->implode(fn ($user) => $user['name'].'-'.$user['email'], ','));
}

#[DataProvider('collectionClassProvider')]
public function testImplodeStringable($collection)
{
$data = new $collection([new ByteString('taylor'), new ByteString('dayle')]);
$this->assertSame('taylordayle', $data->implode(''));
}

#[DataProvider('collectionClassProvider')]
public function testImplodeModels($collection)
{
Expand Down
Loading