Backend Development 6 min read

Essential Laravel Helper Functions for PHP Developers

This article introduces a collection of practical Laravel helper functions—including Str::limit, head, last, Str::between, blank, Str::contains, Arr::pluck, and collect—explaining their purpose, usage, and providing code examples to help PHP developers write cleaner, more efficient backend code.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Essential Laravel Helper Functions for PHP Developers

As a Laravel‑focused PHP full‑stack developer, I often look for built‑in helper methods that can reduce development time and simplify code.

Str::limit() truncates a string to a specified length and optionally appends a custom ending.

use Illuminate\Support\Str;

$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);
// The quick brown fox ...

$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, '[...]');
// The quick brown fox [...]

head() returns the first element of an array.

$array = [100, 200, 300];
$first = head($array);
// 100

last() returns the last element of an array.

$array = [100, 200, 300];
$last = last($array);
// 300

Str::between() extracts the portion of a string that lies between two given substrings, returning null if the boundaries are not found.

use Illuminate\Support\Str;

$slice = Str::between('My name is Inigo Montoya.', 'My name is ', '.');
// 'Inigo Montoya'

blank() checks whether a value is “empty” in a more expressive way than empty() , returning true for empty strings, whitespace, null , empty collections, etc., and false for non‑empty values.

// true examples
blank('');
blank('   ');
blank(null);
blank(collect());

// false examples
blank(true);
blank(false);
blank(0);

Str::contains() determines if a string contains a given substring, offering a more readable alternative to strpos .

use Illuminate\Support\Str;

$contains = Str::contains('My name is Inigo Montoya.', 'Inigo'); // true
$contains = Str::contains('My name is Inigo Montoya.', 'Andrew'); // false

Arr::pluck() extracts a list of values from a multi‑dimensional array using “dot” notation for the desired key.

use Illuminate\Support\Arr;

$array = [
    ['website' => ['id' => 1, 'url' => 'reddit.com']],
    ['website' => ['id' => 2, 'url' => 'twitter.com']],
    ['website' => ['id' => 3, 'url' => 'dev.to']],
];

$urls = Arr::pluck($array, 'website.url');
// ['reddit.com', 'twitter.com', 'dev.to']

collect() converts an array into a Laravel collection, granting access to a rich set of chainable methods for filtering, mapping, and reducing data without explicit loops.

$collection = collect(['Keys', 'Krates']);

$upper = $collection->map(function ($value) {
    return Str::upper($value);
});
// ['KEYS', 'KRATES']

$filtered = $collection->filter(function ($value) {
    return strlen($value) > 4;
});
// ['Krates']

These helper functions are widely used in real projects to streamline tasks such as generating excerpts, accessing array elements, parsing strings, and manipulating data collections, ultimately leading to cleaner and more maintainable backend code.

backendphpLaravelhelper functionsLaravel ArrLaravel Str
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.