If you find this website useful, your sponsorship would greatly support its ongoing development and maintenance. Learn more

Helpers

AdminTW comes in a collection of helpers, located in app/Http/helpers.php

abort_if_cannot

Abort when user does not have permission to perform the requestion action.

For instance abort users from seeing user activity when they don't have permissions from their given roles. The action is looked up as a boolean, it can be used inside if statements.

When the user does not have permission the message will be printed: You do not have permissions to (permission name)


        
            abort_if_cannot('view_users_activity')
           

The helper takes 2 arguments, the name to check and the status code. The default status code is 401.


        
        abort_if_cannot(string $action, int $code = 401)
           

abort_permission

This performs the same as above but when a permission returns false instead of aborting globally an error view is returned, this is helpful if you want to show a message but don't stop the page execution.


        
        abort_permission(string $action, int $code = 401)
           

add_user_log

Pass an array of data to populate the user log. This adds to the user activity table.

The following keys are supported:

  • Title $data['title'] sets the activity title
  • Link $data['link'] a URL link
  • Reference identifier $data['id'] set a reference normally a model's primary key
  • Section $data['section'] identifty the section ie Users, Posts
  • Type $data['type'] sets the type of activity ie Create, Update, Delete

        
        add_user_log(array $data)
           

Example:


    
    add_user_log([
        'title'        => "updated ".$this->user->name."'s password",
        'reference_id' => $this->user->id,
        'link'         => route('admin.users.edit', ['user' => $this->user->id]),
        'section'      => 'Users',
        'type'         => 'Update'
    ]);
        

can

This checks if a user has a given permission, returns a boolean.


        
        can(string $action)
           

create_avatar

Create an avator by providing a name, normally initials followed by the desired filename and location to be stored.


        
        //generate image
        $name      = get_initials($user->name);
        $id        = $user->id.'.png';
        $path      = 'users/';
        $imagePath = create_avatar($name, $id, $path);

        //save image
        $user->image = $imagePath;
        $user->save();
           

cannot

This checks if a user does not have a given permission, returns a boolean.


        
        cannot(string $action)
           

get_initials

Return the first letter of each word.


        
        get_initials(string $name)
           

in_array_r

Determine if a given item ($needle) is in an array of any of its childrens array. The lookup is recursive.


        
        in_array_r($needle, $haystack, $strict = false)
           

is_admin

This checks if a user has a role of admin, returns a boolean.


        
        is_admin()
           

has_role

This checks if a user has a role, returns a boolean.


        
        has_role(string $roleName)
           

size_readable

Return a user friendly size from a given bytes. In the format of B, kB, MB, GB, TB


        
        size_readable(int $bytes)
           

storage_exists

Check if a $file exists inside Laravel's storage, by default the public disk will be used.


        
        storage_exists($file, $disk = 'public')
           

storage_url

Return a url from storage its the equivalent of Storage::url($file).


        
        storage_url($file, $disk = 'public'):
           

user

Reuturns an instance of the users auth: auth()->user().


        
        user()
           

vat

Return VAT by providing a price and VAT amount.


        
        vat(float $price, int $vat)
           

© 2024 Laravel AdminTW. All rights reserved.

Built by David Carr