Yes, there is another way to do this!
Step 1: Register a custom Blade directive:
<?php // code in app/Providers/AppServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Blade; // <-- This is important! Without it you'll get an exception.
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Make a custom blade directive:
Blade::directive('shout', function ($string) {
return trim(strtoupper($string), '(\'\')');
});
// And another one for good measure:
Blade::directive('customLink', function () {
return '<a href="#">Custom Link</a>';
});
}
...
Step 2: Use your custom Blade directive:
<!-- // code in resources/views/view.blade.php -->
@shout('this is my custom blade directive!!')
<br />
@customLink
Outputs:
THIS IS MY CUSTOM BLADE DIRECTIVE!!
Custom Link
Source: https://laravel.com/docs/5.1/blade#extending-blade
Additional Reading: https://mattstauffer.co/blog/custom-conditionals-with-laravels-blade-directives
If you want to learn how to best make custom classes that you can use anywhere, see Custom Classes in Laravel 5, the Easy Way