|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Migrations\Migration; |
|
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
5
|
|
|
use Illuminate\Support\Facades\Schema; |
|
6
|
|
|
|
|
7
|
|
|
class SeedAdminRoleAndUser extends Migration |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Run the migrations. |
|
11
|
|
|
* |
|
12
|
|
|
* @return void |
|
13
|
|
|
*/ |
|
14
|
|
|
public function up() |
|
15
|
|
|
{ |
|
16
|
|
|
if (Schema::hasTable('roles')) { |
|
17
|
|
|
DB::transaction(function () { |
|
18
|
|
|
$role = DB::table('roles') |
|
19
|
|
|
->where('name',"=", "administrator") |
|
20
|
|
|
->where("guard_name","=", "web") |
|
21
|
|
|
->first(); |
|
22
|
|
|
if (!$role) { |
|
23
|
|
|
$roleId = DB::table('roles')->insertGetId([ |
|
24
|
|
|
"name" => "administrator", |
|
25
|
|
|
"guard_name" => "web", |
|
26
|
|
|
"created_at" => now(), |
|
27
|
|
|
"updated_at" => now(), |
|
28
|
|
|
]); |
|
29
|
|
|
} else { |
|
30
|
|
|
$roleId = $role->id; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$user = DB::table('users')->where("email","=","[email protected]")->first(); |
|
34
|
|
|
if (!$user) { |
|
35
|
|
|
$userId = DB::table('users')->insertGetId([ |
|
36
|
|
|
"name" => "Administrator", |
|
37
|
|
|
"email" => "[email protected]", |
|
38
|
|
|
"password" => Hash::make("password"), |
|
39
|
|
|
"created_at" => now(), |
|
40
|
|
|
"updated_at" => now(), |
|
41
|
|
|
"email_verified_at" => now(), |
|
42
|
|
|
]); |
|
43
|
|
|
} else { |
|
44
|
|
|
$userId = $user->id; |
|
45
|
|
|
} |
|
46
|
|
|
DB::table('model_has_roles')->insert([ |
|
47
|
|
|
"role_id" => $roleId, |
|
48
|
|
|
"model_id" => $userId, |
|
49
|
|
|
"model_type" => \App\Models\User::class, |
|
50
|
|
|
]); |
|
51
|
|
|
}); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Reverse the migrations. |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function down() |
|
61
|
|
|
{ |
|
62
|
|
|
if (Schema::hasTable('roles')) { |
|
63
|
|
|
DB::transaction(function () { |
|
64
|
|
|
$role = DB::table('roles') |
|
65
|
|
|
->where('name',"=", "administrator") |
|
66
|
|
|
->where("guard_name","=", "web") |
|
67
|
|
|
->first(); |
|
68
|
|
|
$userBuilder = DB::table('users') |
|
69
|
|
|
->where("email","=","[email protected]"); |
|
70
|
|
|
$user = $userBuilder->first(); |
|
71
|
|
|
if ($role && $user) { |
|
72
|
|
|
DB::table('model_has_roles') |
|
73
|
|
|
->where("role_id","=", $role->id) |
|
74
|
|
|
->where("model_id","=", $user->id) |
|
75
|
|
|
->where("model_type","=", \App\Models\User::class) |
|
76
|
|
|
->delete(); |
|
77
|
|
|
; |
|
78
|
|
|
$userBuilder->delete(); |
|
79
|
|
|
} |
|
80
|
|
|
}); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|