How to create your own helper functions in Laravel 8?
Hello everyone! I'm new to Laravel (version 8) and trying to figure out how to create custom helper functions. Built-in functions like `str_slug()` or `route()` work great, but I need to add my own.
Situation: in my project, I often format dates and prices. In every controller, I have to write the same code:
```php
// Price formatting
$price = number_format($product->price, 2, ',', ' ');
// Date formatting
$date = Carbon::parse($product->created_at)->format('d.m.Y');
```
This is extremely annoying. I want to make something like `formatPrice($value)` and `formatDate($date)` so I can call them anywhere — in controllers, models, Blade templates.
I tried simply creating a file `app/helpers.php` and writing functions there. They work in controllers, but in Blade templates they throw
No answers yet.
Log in to leave an answer
Blogs