A Helper is a file containing a few small functions that simplify the process. Instead of writing the same code every time you use it in a different location, you can put it here and call upon it whenever you need it.
πΉ format_price($price, $currency = 'EGP')used:
You coordinate the price and add the currency at the end.
π code:
function format_price($price, $currency = 'EGP') {
return number_format($price, 2) . ' ' . $currency;
}
πΉ limit_text($text, $limit = 50)used:
To shorten long text and add ... if it exceeds the limit.
π code:
use Illuminate\Support\Str;
function limit_text($text, $limit = 50) {
return strlen($text) > $limit ? substr($text, 0, $limit) . '...' : $text;
}
πΉ time_ago($date)used:
To show how long ago a date was (e.g., "2 hours ago", "3 days ago").
π code:
use Carbon\Carbon;
function time_ago($date) {
return Carbon::parse($date)->diffForHumans();
}
uuid()