Skip to content

Commit

Permalink
[9.x] Added callback support on implode Collection method. (#41405)
Browse files Browse the repository at this point in the history
* [9.x] Added callback support on implode Collection method.

Allow to use a callback as `$value` on `implode` Collection method to simplify -`>map()->implode()` calls:

Before:

```php
<span>{{ $user->cities->map(fn ($city) => $city->name.' ('.$city->state->name.')')->implode(', ') }}</span>
```

After:

```php
<span>{{ $user->cities->implode(fn ($city) => $city->name.' ('.$city->state->name.')', ', ') }}</span>
```

* Added implode callback test
  • Loading branch information
eusonlito committed Mar 9, 2022
1 parent 22c4bb1 commit c5f9518
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,16 @@ public function hasAny($key)
/**
* Concatenate values of a given key as a string.
*
* @param string $value
* @param callable|string $value
* @param string|null $glue
* @return string
*/
public function implode($value, $glue = null)
{
if (is_callable($value)) {
return implode($glue ?? '', $this->map($value)->all());
}

$first = $this->first();

if (is_array($first) || (is_object($first) && ! $first instanceof Stringable)) {
Expand Down

0 comments on commit c5f9518

Please sign in to comment.