Marijan Gudelj

So you are working on a really big project and you have a bunch of stuff that needs to be crunched in the blade template. But how to accomplish that? This is a fast tutorial just to give you some ideas.

So as We all know Laravel likes to keep things clean. But you have a lot of stuff that still needs to be crunched in blade templates. Well this is a small tutorial how I do things.

First create an helper

in the app folder in laravel create an Helpers folder. Inside create an MyHelper.php file with an class.
After that go to config/app.php and create an alias for your helper. It should look something like this:

'MyHelper' => App\Helpers\MyHelper::class,

You added an alias so you can call your helper file in the Blade view.

Next create your function

so go ahead and declare your function. I prefer that the functions are static in helpers

public static function foo(){
  return 'bar';

Now how do I use that function?

Easy part:
Go to your blade file and just write

 {!! MyHelper::foo() !!} 

The {!! !!} syntax is used to display unescaped data.


Use your Helpers to crunch any data: maybe you don’t have an roles and permissions system out of the box (like Spaties package) or you want to return a string and you want to keep your code clean.
Pass variables, check models. Do anything you fancy. The sky is the limit.
Happy coding