Skip to content

Instantly share code, notes, and snippets.

@mknparreira
Last active August 12, 2021 22:28
Show Gist options
  • Save mknparreira/0a95e5834c15e3d6ccfafdc6d4c53b6c to your computer and use it in GitHub Desktop.
Save mknparreira/0a95e5834c15e3d6ccfafdc6d4c53b6c to your computer and use it in GitHub Desktop.
Laravel | Creating own Laravel directives

Laravel Directives

This is a example to create a personal laravel directive

use Illuminate\View\Compilers\BladeCompiler;

protected function registerBladeExtensions()
    {
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
            $bladeCompiler->if('canActAsTgo', function ($permission_user) {
                if (\Gate::allows('onlyOwner')) {
                    return true;
                }

                $getUser =  auth()->user()->asTgoUserInsideAccount;
                if(!isset($getUser)){
                    return false;
                }
                $arr = [];
                foreach ($getUser->permissions as $permission){
                    if($permission->name == $permission_user){
                        $arr = ["name" => $permission->name];
                    }
                }
                $newCollect = collect($arr);
                return $newCollect->contains($permission_user);
            });
        });
    }

   $this->registerBladeExtensions();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment