Skip to content

Commit 6047a4c

Browse files
committed
commit regradin user guide
1 parent d863ce8 commit 6047a4c

File tree

1 file changed

+116
-1
lines changed

1 file changed

+116
-1
lines changed

README.md

+116-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,116 @@
1-
# laravel-simple-permissions
1+
# Laravel Simple Permissions
2+
3+
A simple Laravel package for handling roles and permissions. This package allows you to assign roles to users, manage permissions, and easily check for roles and permissions in your application.
4+
5+
## Installation
6+
7+
To install the package, run the following command:
8+
9+
```bash
10+
composer require lazycode/laravel-simple-permissions:@dev
11+
```
12+
### Publish Configuration
13+
After installing the package, publish the configuration file using:
14+
15+
16+
```bash
17+
php artisan vendor:publish --provider="Lazycode\Permissions\PermissionsServiceProvider" --tag=config
18+
```
19+
20+
### Run Migrations
21+
Next, run the migrations to create the necessary tables in your database:
22+
```bash
23+
php artisan migrate
24+
```
25+
26+
## Usage
27+
### Setting Up the User Model
28+
In your User model (usually located at app/Models/User.php), ensure you include the HasRolesAndPermissions trait:
29+
```php
30+
<?php
31+
32+
namespace App\Models;
33+
34+
use Illuminate\Database\Eloquent\Factories\HasFactory;
35+
use Illuminate\Foundation\Auth\User as Authenticatable;
36+
use Illuminate\Notifications\Notifiable;
37+
use Laravel\Sanctum\HasApiTokens;
38+
use Lazycode\Permissions\Traits\HasRolesAndPermissions; // Ensure this is correct
39+
40+
class User extends Authenticatable
41+
{
42+
use HasApiTokens, HasFactory, Notifiable, HasRolesAndPermissions;
43+
44+
// Your existing model code...
45+
}
46+
47+
```
48+
49+
### Assigning Roles and Permissions
50+
You can now use the package's functionality in your application. Here are some examples:
51+
52+
#### Assign a Role
53+
To assign a role to a user:
54+
55+
```php
56+
$user = User::find(1); // Replace with the appropriate user ID
57+
$user->assignRoleByName('Admin');
58+
```
59+
60+
#### Get User's Role
61+
To get the user's assigned role:
62+
```php
63+
$role = $user->getRole();
64+
```
65+
66+
#### Check User's Permissions
67+
To check if a user has a specific permission:
68+
```php
69+
if ($user->hasPermission('edit-user')) {
70+
// The user has permission to edit a user
71+
}
72+
```
73+
74+
## Seeding Roles and Permissions
75+
You can create a seeder to start with some default roles and permissions. For example:
76+
77+
```php
78+
use Illuminate\Database\Seeder;
79+
use Lazycode\Permissions\Models\Role;
80+
use Lazycode\Permissions\Models\Permission;
81+
use App\Models\User;
82+
83+
class RolesAndPermissionsSeeder extends Seeder
84+
{
85+
public function run()
86+
{
87+
// Create permissions
88+
$editUserPermission = Permission::create(['name' => 'edit-user']);
89+
$addUserPermission = Permission::create(['name' => 'add-user']);
90+
$deleteUserPermission = Permission::create(['name' => 'delete-user']);
91+
92+
// Create a role
93+
$adminRole = Role::create(['name' => 'Admin']);
94+
95+
// Attach permissions to the role
96+
$adminRole->permissions()->attach([$editUserPermission->id, $addUserPermission->id, $deleteUserPermission->id]);
97+
98+
// Create a user and assign the role
99+
$user = User::create(['name' => 'Admin User', 'email' => '[email protected]', 'password' => bcrypt('password')]);
100+
$user->assignRoleByName('Admin');
101+
}
102+
}
103+
```
104+
105+
## Database Schema
106+
Ensure your database schema is set up correctly. The following tables should exist:
107+
108+
- `roles`: To store roles.
109+
- `permissions`: To store permissions.
110+
- `role_permission`: Pivot table to manage many-to-many relationships between roles and permissions.
111+
- `users`: Ensure there is a `role_id` column in the `users` table to associate users with their roles.
112+
113+
## Conclusion
114+
This package provides a simple way to manage user roles and permissions in your Laravel application. For further customization or additional features, feel free to contribute or reach out for support.
115+
116+

0 commit comments

Comments
 (0)