Name: Laravel Helper Functions
Description: Always prefer Laravel's built-in helper classes over native PHP functions. Use Arr::, Str::, and Collection methods instead of native PHP equivalents.
Compatible Agents: general-purpose, backend
Tags: app/**/*.php, laravel, php, backend, helpers, arr, str, collection
Rules
- Always prefer Laravel's built-in helper classes over native PHP functions
- Arrays: Use
Arr:: (import Illuminate\Support\Arr) — never use array_map, array_filter, in_array, array_key_exists, etc. when a Laravel equivalent exists
- Strings: Use
Str:: (import Illuminate\Support\Str) — never use substr, strpos, str_replace, strtolower, etc. when a Laravel equivalent exists
- Collections: Prefer
collect() + Collection methods over manual array loops
- Use
Str::of() for chaining multiple string operations
- Use
collect() to wrap any array or iterable when performing multiple operations
Examples
// Arrays
use Illuminate\Support\Arr;
// ✓ Dot-notation access
Arr::get($array, 'user.address.city', 'Unknown');
// ❌ $array['user']['address']['city'] ?? 'Unknown'
// ✓ Keep only specific keys
Arr::only($array, ['name', 'email']);
// ❌ array_intersect_key(...)
// ✓ Remove specific keys
Arr::except($array, ['password', 'token']);
// ❌ unset($array['key'])
// ✓ Map values
Arr::map($array, fn($value) => strtoupper($value));
// ❌ array_map(fn($v) => strtoupper($v), $array)
// Strings
use Illuminate\Support\Str;
Str::upper('hello'); // ❌ strtoupper('hello')
Str::lower('HELLO'); // ❌ strtolower('HELLO')
Str::slug('Hello World'); // ❌ strtolower(str_replace(' ', '-', $string))
Str::contains('foo bar', 'foo'); // ❌ str_contains()
Str::random(32); // ❌ bin2hex(random_bytes(16))
// Fluent chaining
$result = Str::of(' Hello World ')
->trim()
->lower()
->slug();
// Collections
$users = collect(User::all());
// ✓ Prefer collection methods
$admins = $users->filter(fn($u) => $u->isAdmin())->values();
$names = $users->pluck('name');
$byRole = $users->groupBy('role');
// ❌ manual foreach loops
Quick Reference: PHP → Laravel
| PHP native |
Laravel equivalent |
array_map($fn, $arr) |
Arr::map($arr, $fn) |
array_filter($arr, $fn) |
Arr::where($arr, $fn) |
array_key_exists($k, $arr) |
Arr::exists($arr, $k) |
in_array($v, $arr) |
collect($arr)->contains($v) |
strtolower($str) |
Str::lower($str) |
strtoupper($str) |
Str::upper($str) |
str_contains($str, $sub) |
Str::contains($str, $sub) |
http_build_query($arr) |
Arr::query($arr) |
Anti-Patterns
- Using
array_map, array_filter, array_key_exists when Arr:: equivalents exist
- Using
strtolower, strtoupper, str_replace when Str:: equivalents exist
- Writing manual
foreach loops when a Collection method achieves the same result
- Not importing
Arr or Str before use — always add the use statement
References
1---2name: helperfunctions3description: Always prefer Laravel's built-in helper classes over native PHP functions. Use `Arr::`, `Str::`, and Collection methods instead of native PHP equivalents.4---5
6**Name:** Laravel Helper Functions
7**Description:** Always prefer Laravel's built-in helper classes over native PHP functions. Use `Arr::`, `Str::`, and Collection methods instead of native PHP equivalents.
8**Compatible Agents:** general-purpose, backend
9**Tags:** app/**/*.php, laravel, php, backend, helpers, arr, str, collection
10
11## Rules
12
13- Always prefer Laravel's built-in helper classes over native PHP functions
14- **Arrays**: Use `Arr::` (import `Illuminate\Support\Arr`) — never use `array_map`, `array_filter`, `in_array`, `array_key_exists`, etc. when a Laravel equivalent exists
15- **Strings**: Use `Str::` (import `Illuminate\Support\Str`) — never use `substr`, `strpos`, `str_replace`, `strtolower`, etc. when a Laravel equivalent exists
16- **Collections**: Prefer `collect()` + Collection methods over manual array loops
17- Use `Str::of()` for chaining multiple string operations
18- Use `collect()` to wrap any array or iterable when performing multiple operations
19
20## Examples
21
22```php
23// Arrays
24use Illuminate\Support\Arr;
25
26// ✓ Dot-notation access
27Arr::get($array, 'user.address.city', 'Unknown');
28// ❌ $array['user']['address']['city'] ?? 'Unknown'
29
30// ✓ Keep only specific keys
31Arr::only($array, ['name', 'email']);
32// ❌ array_intersect_key(...)
33
34// ✓ Remove specific keys
35Arr::except($array, ['password', 'token']);
36// ❌ unset($array['key'])
37
38// ✓ Map values
39Arr::map($array, fn($value) => strtoupper($value));
40// ❌ array_map(fn($v) => strtoupper($v), $array)
41```
42
43```php
44// Strings
45use Illuminate\Support\Str;
46
47Str::upper('hello'); // ❌ strtoupper('hello')
48Str::lower('HELLO'); // ❌ strtolower('HELLO')
49Str::slug('Hello World'); // ❌ strtolower(str_replace(' ', '-', $string))
50Str::contains('foo bar', 'foo'); // ❌ str_contains()
51Str::random(32); // ❌ bin2hex(random_bytes(16))
52
53// Fluent chaining
54$result = Str::of(' Hello World ')
55 ->trim()
56 ->lower()
57 ->slug();
58```
59
60```php
61// Collections
62$users = collect(User::all());
63
64// ✓ Prefer collection methods
65$admins = $users->filter(fn($u) => $u->isAdmin())->values();
66$names = $users->pluck('name');
67$byRole = $users->groupBy('role');
68
69// ❌ manual foreach loops
70```
71
72**Quick Reference: PHP → Laravel**
73
74| PHP native | Laravel equivalent |
75|---|---|
76| `array_map($fn, $arr)` | `Arr::map($arr, $fn)` |
77| `array_filter($arr, $fn)` | `Arr::where($arr, $fn)` |
78| `array_key_exists($k, $arr)` | `Arr::exists($arr, $k)` |
79| `in_array($v, $arr)` | `collect($arr)->contains($v)` |
80| `strtolower($str)` | `Str::lower($str)` |
81| `strtoupper($str)` | `Str::upper($str)` |
82| `str_contains($str, $sub)` | `Str::contains($str, $sub)` |
83| `http_build_query($arr)` | `Arr::query($arr)` |
84
85## Anti-Patterns
86
87- Using `array_map`, `array_filter`, `array_key_exists` when `Arr::` equivalents exist
88- Using `strtolower`, `strtoupper`, `str_replace` when `Str::` equivalents exist
89- Writing manual `foreach` loops when a Collection method achieves the same result
90- Not importing `Arr` or `Str` before use — always add the `use` statement
91
92## References
93
94- [Laravel Arr Helper](https://laravel.com/docs/helpers#arrays)
95- [Laravel Str Helper](https://laravel.com/docs/helpers#strings)
96- [Laravel Collections](https://laravel.com/docs/collections)