Skip to content

Instantly share code, notes, and snippets.

@mrunknown0001
Last active August 30, 2025 04:59
Show Gist options
  • Select an option

  • Save mrunknown0001/cb8f90824ade6c307b3d27423e9a1690 to your computer and use it in GitHub Desktop.

Select an option

Save mrunknown0001/cb8f90824ade6c307b3d27423e9a1690 to your computer and use it in GitHub Desktop.
Laravel helper file that contains common functions

How to Use the Gist in Laravel:

  1. Save the Gist as helpers.php.

  2. Place the file in the app/Helpers directory (create the directory if it doesn't exist).

  3. Add the following line to your composer.json file to autoload the helpers:

"autoload": {
    "files": [
        "app/Helpers/helpers.php"
    ]
}
  1. Run composer dump-autoload to refresh the autoloader.

  2. Use the helper functions anywhere in your Laravel application.

Example Usage

$randomString = generate_random_string(20);
echo $randomString;

$formattedDate = format_date(now(), 'F j, Y');
echo $formattedDate;

$slug = slugify('Hello World!');
echo $slug;

if (is_production()) {
    echo 'Running in production mode.';
}

$csvData = [
    ['Name', 'Email'],
    ['John Doe', '[email protected]'],
];
echo array_to_csv($csvData);

try {
    // Some code that may throw an exception
} catch (\Throwable $e) {
    log_error($e, 'Failed to process request');
}

This Gist provides reusable helper functions that can save time for Laravel developers. You can customize it further based on your needs!

<?php
if (!function_exists('generate_random_string')) {
/**
* Generate a random string of a specified length.
*
* @param int $length
* @return string
*/
function generate_random_string($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
}
if (!function_exists('format_date')) {
/**
* Format a date to a human-readable format.
*
* @param string $date
* @param string $format
* @return string
*/
function format_date($date, $format = 'Y-m-d H:i:s')
{
return \Carbon\Carbon::parse($date)->format($format);
}
}
if (!function_exists('slugify')) {
/**
* Convert a string to a URL-friendly slug.
*
* @param string $text
* @return string
*/
function slugify($text)
{
return \Illuminate\Support\Str::slug($text);
}
}
if (!function_exists('is_production')) {
/**
* Check if the application is in production mode.
*
* @return bool
*/
function is_production()
{
return config('app.env') === 'production';
}
}
if (!function_exists('array_to_csv')) {
/**
* Convert an array to a CSV string.
*
* @param array $data
* @return string
*/
function array_to_csv(array $data)
{
$output = fopen('php://temp', 'r+');
foreach ($data as $row) {
fputcsv($output, $row);
}
rewind($output);
$csv = stream_get_contents($output);
fclose($output);
return $csv;
}
}
if (!function_exists('log_error')) {
/**
* Log an error with a custom message.
*
* @param \Throwable $exception
* @param string $message
* @return void
*/
function log_error(\Throwable $exception, $message = 'An error occurred')
{
\Log::error($message, [
'error' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
]);
}
}
if (!function_exists('humanize_date')) {
/**
* Format a date to a human-readable format.
*
* @param string $date
* @param string $format
* @return string
*/
function humanize_date($date)
{
return Carbon\Carbon::parse($date)->diffForHumans();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment