lundi 8 octobre 2018

Error in Custom Blade Directives with array as parameter

In my Laravel 5.7 app I want to use Custom Blade Directives and to pass an array in this directive as there could be different access, like :

@loggedUserHasAccess([USER_ACCESS_ADMIN])
<div class="col">
    <a class="social-inner" href="" >
        <span class="icon"></span><span>Backend</span>
    </a>
</div>
@endLoggedUserHasAccess

And in app/Providers/AppServiceProvider.php :

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {

        ...


        \Blade::directive('loggedUserHasAccess', function (array $accessArray) {
            $condition = false;

            if (Auth::check()) {
                $loggedUser  = Auth::user();
                $usersGroups = User::getUsersGroupsByUserId($loggedUser->id, false);
                foreach ($usersGroups as $next_key => $nextUsersGroup) {
                    if (in_array($nextUsersGroup->group_id, $accessArray)) {
                        $condition = true;
                    }
                }
            }
            return "<?php if ($condition) { ?>";
        });

        Blade::directive('endLoggedUserHasAccess', function () {
            return "<?php } ?>";
        });

But I got syntax error : https://imgur.com/a/I5s1TmQ

USER_ACCESS_ADMIN is defined in bootstrap/app.php. looks like my syntax is invalid, but which is valid ?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire