Skip to content

Commit d863ce8

Browse files
committed
update the functionality
1 parent 6f75fcc commit d863ce8

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

src/Models/Permission.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

3-
namespace Lazycode\LaravelSimplePermission\Models;
3+
namespace Lazycode\Permissions\Models;
44

55
use Illuminate\Database\Eloquent\Model;
66

77
class Permission extends Model
88
{
99
protected $fillable = ['name'];
10-
10+
1111
public function roles()
1212
{
13-
return $this->belongsToMany(Role::class);
13+
return $this->belongsToMany(Role::class, 'role_permission');
1414
}
1515
}

src/Models/Role.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Lazycode\LaravelSimplePermission\Models;
3+
namespace Lazycode\Permissions\Models;
44

55
use Illuminate\Database\Eloquent\Model;
66

@@ -10,6 +10,6 @@ class Role extends Model
1010

1111
public function permissions()
1212
{
13-
return $this->belongsToMany(Permission::class);
13+
return $this->belongsToMany(Permission::class, 'role_permission');
1414
}
1515
}

src/database/migrations/create_permission_tables.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ public function up()
3232
});
3333
}
3434

35-
if (!Schema::hasTable('users')) {
36-
Schema::table('users', function (Blueprint $table) {
37-
$table->foreignId('role_id')->nullable()->constrained('roles')->onDelete('set null');
38-
});
35+
if (Schema::hasTable('users')) {
36+
if (!Schema::hasColumn('users', 'role_id')) {
37+
Schema::table('users', function (Blueprint $table) {
38+
$table->foreignId('role_id')->nullable()->constrained('roles')->onDelete('set null');
39+
});
40+
}
3941
}
4042

4143
}
@@ -45,8 +47,11 @@ public function down()
4547
Schema::dropIfExists('role_permission');
4648
Schema::dropIfExists('permissions');
4749
Schema::dropIfExists('roles');
48-
Schema::table('users', function (Blueprint $table) {
49-
$table->dropForeign(['role_id']);
50-
});
50+
if (Schema::hasTable('users') && Schema::hasColumn('users', 'role_id')) {
51+
Schema::table('users', function (Blueprint $table) {
52+
$table->dropForeign(['role_id']);
53+
$table->dropColumn('role_id');
54+
});
55+
}
5156
}
5257
};

0 commit comments

Comments
 (0)