Skip to content

Commit f108ebf

Browse files
committed
Document with() helper function
1 parent 26394a1 commit f108ebf

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

helpers.md

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
170170
[validator](#method-validator)
171171
[value](#method-value)
172172
[view](#method-view)
173+
[with](#method-with)
173174

174175
</div>
175176

@@ -709,7 +710,7 @@ The `camel_case` function converts the given string to `camelCase`:
709710
<a name="method-class-basename"></a>
710711
#### `class_basename()` {#collection-method}
711712

712-
-The `class_basename` returns the class name of the given class with the class' namespace removed:
713+
The `class_basename` returns the class name of the given class with the class' namespace removed:
713714

714715
$class = class_basename('Foo\Bar\Baz');
715716

@@ -833,7 +834,7 @@ The `str_is` function determines if a given string matches a given pattern. Aste
833834
<a name="method-str-limit"></a>
834835
#### `str_limit()` {#collection-method}
835836

836-
The `str_limit` function limits the number of characters in a string. The function accepts a string as its first argument and the maximum number of resulting characters as its second argument:
837+
The `str_limit` function truncates the given string at the specified length:
837838

838839
$truncated = str_limit('The quick brown fox jumps over the lazy dog', 20);
839840

@@ -1159,7 +1160,7 @@ You may add items to the cache by passing an array of key / value pairs to the f
11591160
<a name="method-class-uses-recursive"></a>
11601161
#### `class_uses_recursive()` {#collection-method}
11611162

1162-
The `class_uses_recursive()` function returns all traits used by a class, including traits used by any subclasses:
1163+
The `class_uses_recursive` function returns all traits used by a class, including traits used by any subclasses:
11631164

11641165
$traits = class_uses_recursive(App\User::class);
11651166

@@ -1509,26 +1510,26 @@ The `throw_unless` function throws the given exception if a given boolean expres
15091510
<a name="method-trait-uses-recursive"></a>
15101511
#### `trait_uses_recursive()` {#collection-method}
15111512

1512-
The `trait_uses_recursive()` function returns all traits used by a trait:
1513+
The `trait_uses_recursive` function returns all traits used by a trait:
15131514

15141515
$traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);
15151516

15161517
<a name="method-transform"></a>
15171518
#### `transform()` {#collection-method}
15181519

1519-
The `transform` function executes a Closure on a given value if the value is not [blank](#method-blank) and returns the result of the Closure:
1520+
The `transform` function executes a `Closure` on a given value if the value is not [blank](#method-blank) and returns the result of the `Closure`:
15201521

1521-
transform(5, function ($value) {
1522+
$callback = function ($value) {
15221523
return $value * 2;
1523-
});
1524+
};
1525+
1526+
$result = transform(5, $callback);
15241527

15251528
// 10
15261529

1527-
A default value or Closure may also be passed as the third parameter to the method. This value will be returned if the given value is blank:
1530+
A default value or `Closure` may also be passed as the third parameter to the method. This value will be returned if the given value is blank:
15281531

1529-
transform(null, function ($value) {
1530-
return $value * 2;
1531-
}, 'The value is blank');
1532+
$result = transform(null, $callback, 'The value is blank');
15321533

15331534
// The value is blank
15341535

@@ -1542,17 +1543,42 @@ The `validator` function creates a new [validator](/docs/{{version}}/validation)
15421543
<a name="method-value"></a>
15431544
#### `value()` {#collection-method}
15441545

1545-
The `value` function's behavior will simply return the value it is given. However, if you pass a `Closure` to the function, the `Closure` will be executed then its result will be returned:
1546+
The `value` function returns the value it is given. However, if you pass a `Closure` to the function, the `Closure` will be executed then its result will be returned:
15461547

1547-
$value = value(function () {
1548-
return 'bar';
1548+
$result = value(true);
1549+
1550+
// true
1551+
1552+
$result = value(function () {
1553+
return false;
15491554
});
15501555

1551-
// bar
1556+
// false
15521557

15531558
<a name="method-view"></a>
15541559
#### `view()` {#collection-method}
15551560

15561561
The `view` function retrieves a [view](/docs/{{version}}/views) instance:
15571562

15581563
return view('auth.login');
1564+
1565+
<a name="method-with"></a>
1566+
#### `with()` {#collection-method}
1567+
1568+
The `with` function returns the value it is given. However, if you also pass a `Closure` as a second argument to the function, the `Closure` will be executed then its result will be returned:
1569+
1570+
$callback = function ($value) {
1571+
return (is_numeric($value)) ? $value * 2 : 0;
1572+
};
1573+
1574+
$result = with(5, $callback);
1575+
1576+
// 10
1577+
1578+
$result = with(null, $callback);
1579+
1580+
// 0
1581+
1582+
$result = with(5, null);
1583+
1584+
// 5

0 commit comments

Comments
 (0)