The XHelper class in the LaraUtilX package provides a collection of static helper methods for common operations including array manipulation, string processing, date formatting, and UUID generation.
arrayTrim(array $array): array
arrayFlatten(array $array): array
strBetween(string $string, string $start, string $end): ?string
strSlugify(string $string): string
carbonParse($date, $format = 'Y-m-d H:i:s'): ?string
carbonHumanDiff($date): string
uuid(): string
To use the XHelper class, import it at the top of your file:
use LaraUtilX\Helpers\XHelper;
All methods are static, so you can call them directly on the class without instantiation.
Trims whitespace from all string values in an array.
Example:
use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class TestController extends Controller
{
public function testArrayTrim(Request $request): JsonResponse
{
$input = $request->input('array', [' hello ', ' world ', ' test ']);
$result = XHelper::arrayTrim($input);
return response()->json([
'function' => 'arrayTrim',
'input' => $input,
'output' => $result,
'description' => 'Trims whitespace from all string values in an array'
]);
}
}
Result:
{
"function": "arrayTrim",
"input": [" hello ", " world ", " test "],
"output": ["hello", "world", "test"],
"description": "Trims whitespace from all string values in an array"
}
Flattens a multi-dimensional array into a single-dimensional array.
Example:
use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class TestController extends Controller
{
public function testArrayFlatten(Request $request): JsonResponse
{
$input = [
'users' => [
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane']
],
'posts' => [
['id' => 1, 'title' => 'Post 1'],
['id' => 2, 'title' => 'Post 2']
]
];
$result = XHelper::arrayFlatten($input);
return response()->json([
'function' => 'arrayFlatten',
'input' => $input,
'output' => $result,
'description' => 'Flattens a multi-dimensional array into a single-dimensional array'
]);
}
}
Result:
{
"function": "arrayFlatten",
"input": {
"users": [{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}],
"posts": [{"id": 1, "title": "Post 1"}, {"id": 2, "title": "Post 2"}]
},
"output": [{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}, {"id": 1, "title": "Post 1"}, {"id": 2, "title": "Post 2"}],
"description": "Flattens a multi-dimensional array into a single-dimensional array"
}
Extracts a substring between two specified strings.
Example:
use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class TestController extends Controller
{
public function testStrBetween(Request $request): JsonResponse
{
$string = 'Hello [world] from Laravel';
$start = '[';
$end = ']';
$result = XHelper::strBetween($string, $start, $end);
return response()->json([
'function' => 'strBetween',
'input' => [
'string' => $string,
'start' => $start,
'end' => $end
],
'output' => $result,
'description' => 'Extracts a substring between two specified strings'
]);
}
}
Result:
{
"function": "strBetween",
"input": {
"string": "Hello [world] from Laravel",
"start": "[",
"end": "]"
},
"output": "world",
"description": "Extracts a substring between two specified strings"
}
Converts a string to a URL-friendly slug format.
Example:
use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class TestController extends Controller
{
public function testStrSlugify(Request $request): JsonResponse
{
$input = 'Hello World! This is a Test String 123';
$result = XHelper::strSlugify($input);
return response()->json([
'function' => 'strSlugify',
'input' => $input,
'output' => $result,
'description' => 'Converts a string to a URL-friendly slug format'
]);
}
}
Result:
{
"function": "strSlugify",
"input": "Hello World! This is a Test String 123",
"output": "hello-world-this-is-a-test-string-123",
"description": "Converts a string to a URL-friendly slug format"
}
Parses a date and formats it using Carbon.
Example:
use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class TestController extends Controller
{
public function testCarbonParse(Request $request): JsonResponse
{
$date = '2024-01-15 14:30:00';
$format = 'Y-m-d';
$result = XHelper::carbonParse($date, $format);
return response()->json([
'function' => 'carbonParse',
'input' => [
'date' => $date,
'format' => $format
],
'output' => $result,
'description' => 'Parses a date and formats it using Carbon'
]);
}
}
Result:
{
"function": "carbonParse",
"input": {
"date": "2024-01-15 14:30:00",
"format": "Y-m-d"
},
"output": "2024-01-15",
"description": "Parses a date and formats it using Carbon"
}
Returns a human-readable difference between a date and now.
Example:
use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class TestController extends Controller
{
public function testCarbonHumanDiff(Request $request): JsonResponse
{
$date = now()->subDays(5);
$result = XHelper::carbonHumanDiff($date);
return response()->json([
'function' => 'carbonHumanDiff',
'input' => $date->toDateTimeString(),
'output' => $result,
'description' => 'Returns a human-readable difference between a date and now'
]);
}
}
Result:
{
"function": "carbonHumanDiff",
"input": "2024-01-10 14:30:00",
"output": "5 days ago",
"description": "Returns a human-readable difference between a date and now"
}
Generates a UUID string.
Example:
use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class TestController extends Controller
{
public function testUuid(Request $request): JsonResponse
{
$result = XHelper::uuid();
return response()->json([
'function' => 'uuid',
'output' => $result,
'description' => 'Generates a UUID string'
]);
}
}
Result:
{
"function": "uuid",
"output": "550e8400-e29b-41d4-a716-446655440000",
"description": "Generates a UUID string"
}
Here's a complete example showing multiple XHelper methods in use:
use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class ExampleController extends Controller
{
public function demonstrateHelpers(Request $request): JsonResponse
{
// Array operations
$trimmed = XHelper::arrayTrim([' hello ', ' world ']);
$flattened = XHelper::arrayFlatten([['a' => 1], ['b' => 2]]);
// String operations
$extracted = XHelper::strBetween('Hello [world]', '[', ']');
$slug = XHelper::strSlugify('Hello World!');
// Date operations
$formatted = XHelper::carbonParse('2024-01-15', 'F j, Y');
$humanDiff = XHelper::carbonHumanDiff(now()->subHours(2));
// UUID generation
$uuid = XHelper::uuid();
return response()->json([
'array_trim' => $trimmed,
'array_flatten' => $flattened,
'str_between' => $extracted,
'str_slugify' => $slug,
'carbon_parse' => $formatted,
'carbon_human_diff' => $humanDiff,
'uuid' => $uuid
]);
}
}
The XHelper class provides convenient static methods for common operations, making it easy to perform array manipulation, string processing, date formatting, and UUID generation throughout your Laravel application.
strSlugify() now transliterates instead of stripping, and accepts a custom separator:
XHelper::strSlugify('Hello Beirut'); // "hello-beirut"
XHelper::strSlugify('مرحبا بيروت'); // "mrhba-byrot"
XHelper::strSlugify('Hello Beirut', '_'); // "hello_beirut"
The previous implementation removed every character outside A-Za-z0-9-, so any non-Latin input, Arabic included, slugified to an empty string.