Skip to content

Commit 04bd255

Browse files
committed
Merge branch 'helper-methods' of https://github.com/jakebathman/docs into jakebathman-helper-methods
2 parents 3ab3e70 + a18109a commit 04bd255

File tree

1 file changed

+156
-1
lines changed

1 file changed

+156
-1
lines changed

helpers.md

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
2222
}
2323
</style>
2424

25-
### Arrays
25+
### Arrays & Objects
2626

2727
<div class="collection-method-list" markdown="1">
2828

@@ -47,6 +47,9 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
4747
[array_sort_recursive](#method-array-sort-recursive)
4848
[array_where](#method-array-where)
4949
[array_wrap](#method-array-wrap)
50+
[data_fill](#method-data-fill)
51+
[data_get](#method-data-get)
52+
[data_set](#method-data-set)
5053
[head](#method-head)
5154
[last](#method-last)
5255
</div>
@@ -123,6 +126,7 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
123126
[auth](#method-auth)
124127
[back](#method-back)
125128
[bcrypt](#method-bcrypt)
129+
[blank](#method-blank)
126130
[broadcast](#method-broadcast)
127131
[cache](#method-cache)
128132
[collect](#method-collect)
@@ -136,6 +140,7 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
136140
[env](#method-env)
137141
[event](#method-event)
138142
[factory](#method-factory)
143+
[filled](#method-filled)
139144
[info](#method-info)
140145
[logger](#method-logger)
141146
[method_field](#method-method-field)
@@ -155,6 +160,7 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
155160
[today](#method-today)
156161
[throw_if](#method-throw-if)
157162
[throw_unless](#method-throw-unless)
163+
[transform](#method-transform)
158164
[validator](#method-validator)
159165
[value](#method-value)
160166
[view](#method-view)
@@ -390,6 +396,12 @@ The `array_random()` function returns a random value from an array:
390396

391397
// 4 - (retrieved randomly)
392398

399+
You may also specify the number of items to return as an optional second parameter. Note that providing this parameter will return an array, even if only one item is desired:
400+
401+
$items = array_random($array, 2);
402+
403+
// $items: [2, 5]
404+
393405
<a name="method-array-set"></a>
394406
#### `array_set()` {#collection-method}
395407

@@ -499,6 +511,96 @@ The `array_wrap` function will wrap the given value in an array. If the given va
499511

500512
// [0 => 'Laravel']
501513

514+
<a name="method-data-fill"></a>
515+
#### `data_fill()` {#collection-method}
516+
517+
The `data_fill` function will fill data in the target array or object where needed, using "dot" notation:
518+
519+
$data = ['foo' => 'bar'];
520+
521+
data_fill($data, 'baz', 'boom')
522+
523+
// ['foo' => 'bar', 'baz' => 'boom']
524+
525+
This function also accepts asterisks as wildcards, and will fill into the target accordingly:
526+
527+
$data = [
528+
'posts' => [
529+
'comments' => [
530+
['name' => 'Taylor'],
531+
[]
532+
],
533+
]
534+
];
535+
536+
data_fill($data,'posts.comments.*.name','No name');
537+
538+
/*
539+
[
540+
'posts' => [
541+
'comments' => [
542+
['name' => 'Taylor'],
543+
['name' => 'No name']
544+
],
545+
]
546+
];
547+
*/
548+
549+
<a name="method-data-get"></a>
550+
#### `data_get()` {#collection-method}
551+
552+
The `data_get` function retrieves a value from a nested array or object using "dot" notation:
553+
554+
$data = ['products' => ['desk' => ['price' => 100]]];
555+
556+
$value = data_get($data, 'products.desk');
557+
558+
// ['price' => 100]
559+
560+
The `data_get` function also accepts a default value, which will be returned if the specific key is not found:
561+
562+
$value = data_get($data, 'names.john', 'default');
563+
564+
<a name="method-data-set"></a>
565+
#### `data_set()` {#collection-method}
566+
567+
The `data_set` function sets a value within a deeply nested array or object using "dot" notation:
568+
569+
$data = ['products' => ['desk' => ['price' => 100]]];
570+
571+
data_set($data, 'products.desk.price', 200);
572+
573+
// ['products' => ['desk' => ['price' => 200]]]
574+
575+
This function also accepts wildcards, and will set values in the target accordingly:
576+
577+
$data = [
578+
'products' => [
579+
['name' => 'desk1', 'price' => 100],
580+
['name' => 'desk2', 'price' => 150],
581+
]
582+
];
583+
584+
data_set($data, 'products.*.price', 200);
585+
586+
/*
587+
[
588+
'products' => [
589+
['name'=> 'desk1' => 'price' => 200],
590+
['name'=> 'desk2' => 'price' => 200],
591+
]
592+
];
593+
*/
594+
595+
By default, any existing values are overwritten. If you wish only set a value if it doesn't exist, you may pass `false` as the third parameter:
596+
597+
$data = ['products' => ['desk' => ['price' => 100]]];
598+
599+
data_set($data, 'products.desk.price', 200, false);
600+
601+
// ['products' => ['desk' => ['price' => 100]]]
602+
603+
502604
<a name="method-head"></a>
503605
#### `head()` {#collection-method}
504606

@@ -999,6 +1101,24 @@ The `broadcast` function [broadcasts](/docs/{{version}}/broadcasting) the given
9991101

10001102
broadcast(new UserRegistered($user));
10011103

1104+
<a name="method-blank"></a>
1105+
#### `blank()` {#collection-method}
1106+
1107+
The `blank` function returns whether the given value is blank":
1108+
1109+
// true
1110+
blank('');
1111+
blank(' ');
1112+
blank(null);
1113+
blank(collect());
1114+
1115+
// false
1116+
blank(0);
1117+
blank(true);
1118+
blank(false);
1119+
1120+
For the inverse of `blank`, see the [filled](#method-filled) method.
1121+
10021122
<a name="method-cache"></a>
10031123
#### `cache()` {#collection-method}
10041124

@@ -1106,6 +1226,24 @@ The `factory` function creates a model factory builder for a given class, name,
11061226

11071227
$user = factory(App\User::class)->make();
11081228

1229+
<a name="method-filled"></a>
1230+
#### `filled()` {#collection-method}
1231+
1232+
The `filled` function returns whether the given value is not blank":
1233+
1234+
// true
1235+
filled(0);
1236+
filled(true);
1237+
filled(false);
1238+
1239+
// false
1240+
filled('');
1241+
filled(' ');
1242+
filled(null);
1243+
filled(collect());
1244+
1245+
For the inverse of `filled`, see the [blank](#method-blank) method.
1246+
11091247
<a name="method-info"></a>
11101248
#### `info()` {#collection-method}
11111249

@@ -1300,6 +1438,23 @@ The `throw_unless` function throws the given exception if a given boolean expres
13001438

13011439
throw_unless(Auth::user()->isAdmin(), AuthorizationException::class, 'You are not allowed to access this page');
13021440

1441+
<a name="method-transform"></a>
1442+
#### `transform()` {#collection-method}
1443+
1444+
The `transform` function executes a Closure on a given value, if the value is not blank, and returns the result of that Closure:
1445+
1446+
transform(5, function ($value) {
1447+
return $value * 2;
1448+
});
1449+
1450+
// 10
1451+
1452+
A default value or Closure may also be passed as the third parameter to the method, which is used if the given value is blank:
1453+
1454+
transform(null, function ($value) {
1455+
return $value * 2;
1456+
}, "Value is blank");
1457+
13031458
<a name="method-validator"></a>
13041459
#### `validator()` {#collection-method}
13051460

0 commit comments

Comments
 (0)