PHP code example of sweet1s / moonshine-roles-permissions
1. Go to this page and download the library: Download sweet1s/moonshine-roles-permissions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sweet1s / moonshine-roles-permissions example snippets
namespace App\Models;
use Sweet1s\MoonshineRBAC\Traits\HasMoonShineRolePermissions;
use Spatie\Permission\Models\Role as SpatieRole;
class Role extends SpatieRole
{
use HasMoonShineRolePermissions;
protected $with = ['permissions'];
}
namespace App\Models;
// ...
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Sweet1s\MoonshineRBAC\Traits\MoonshineRBACHasRoles;
class User extends Authenticatable
{
use MoonshineRBACHasRoles;
const SUPER_ADMIN_ROLE_ID = 1;
// ...
}
namespace App\MoonShine\Resources;
use Sweet1s\MoonshineRBAC\Traits\WithPermissionsFormComponent;
use Sweet1s\MoonshineRBAC\Traits\WithRolePermissions;
class RoleResource extends ModelResource
{
use WithRolePermissions;
use WithPermissionsFormComponent;
// ...
}
namespace App\MoonShine\Resources;
use Sweet1s\MoonshineRBAC\Traits\WithRoleFormComponent;
use Sweet1s\MoonshineRBAC\Traits\WithRolePermissions;
class UserResource extends ModelResource
{
use WithRolePermissions;
use WithRoleFormComponent;
// ...
}
MenuGroup::make('System', [
MenuItem::make('Admins', new \Sweet1s\MoonshineRBAC\Resource\UserResource(), 'heroicons.outline.users'),
MenuItem::make('Roles', new \Sweet1s\MoonshineRBAC\Resource\RoleResource(), 'heroicons.outline.shield-exclamation'),
MenuItem::make('Permissions', new \Sweet1s\MoonshineRBAC\Resource\PermissionResource(), 'heroicons.outline.shield-exclamation'),
], 'heroicons.outline.user-group'),
protected function menu(): array
{
return MenuRBAC::menu(
MenuGroup::make('System', [
MenuItem::make('Admins', new \Sweet1s\MoonshineRBAC\Resource\UserResource(), 'heroicons.outline.users'),
MenuItem::make('Roles', new \Sweet1s\MoonshineRBAC\Resource\RoleResource(), 'heroicons.outline.shield-exclamation'),
], 'heroicons.outline.user-group'),
MenuItem::make(trans('moonshine::general.orders'), new OrderResource(), 'heroicons.outline.shopping-cart')
->badge(function(){
return Order::where('status', Status::Completed->name)->count();
}),
//...
);
}
// ...
use Sweet1s\MoonshineRBAC\Traits\WithRolePermissions;
class PostResource extends ModelResource
{
use WithRolePermissions;
// ...
}