To help us with repetitive tasks like manipulating data for our views and such we can create helper functions to do the dirty work for us. A quick way of doing this is to create a helper class which can be auto loaded by Laravel via the composer.json file. Then we create a static function on our helper class which we can then use directly in our blade templates. Nice one!
This example shows how you can make a function given a string to return the url slug. But your functions can be anything this is just an example.
/app/helpers/helpers.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php use Illuminate\Support\Str; class Helper { public static function makeSlug($str) { return Str::slug($str); } } ?> |
composer.json
1 2 3 4 5 6 7 8 9 10 11 |
"autoload": { "classmap": [ "database", "app/helpers" ], "psr-4": { "App\\": "app/" } }, |
template.blade.php
1 2 3 |
<a href="{{ route('locations.show', $country) }}">/{{ Helper::makeSlug($country) }}</a> |
Credits:
https://howdoi.pl/how-do-i-create-helper-class-with-global-scope-in-laravel-4/
wow thank you man.
you made my day.