1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Migrations\Migration; |
4
|
|
|
use Illuminate\Support\Facades\DB; |
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
|
7
|
|
|
class RemoveBackpackuserModel extends Migration |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Run the migrations. |
11
|
|
|
* |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
|
|
public function up() |
15
|
|
|
{ |
16
|
|
|
// establish the table names |
17
|
|
|
$model_has_roles = config('permission.table_names.model_has_roles'); |
18
|
|
|
$model_has_permissions = config('permission.table_names.model_has_permissions'); |
19
|
|
|
|
20
|
|
|
// replace the BackpackUser model with User |
21
|
|
|
if (\Illuminate\Support\Facades\Schema::hasTable($model_has_roles)) { |
22
|
|
|
$this->replaceModels($model_has_roles); |
23
|
|
|
} |
24
|
|
|
if (\Illuminate\Support\Facades\Schema::hasTable($model_has_permissions)) { |
25
|
|
|
$this->replaceModels($model_has_permissions); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function replaceModels($table_name) |
30
|
|
|
{ |
31
|
|
|
Log::info('Replacing BackpackUser model in '.$table_name); |
32
|
|
|
|
33
|
|
|
// if you've ended up with duplicate entries (both for App\User and App\Models\BackpackUser) |
34
|
|
|
// we can just delete them |
35
|
|
|
$userEntries = DB::table($table_name) |
36
|
|
|
->where('model_type', "App\User") |
37
|
|
|
->get(); |
38
|
|
|
|
39
|
|
|
foreach ($userEntries as $entry) { |
40
|
|
|
DB::table($table_name) |
41
|
|
|
->where('role_id', $entry->role_id) |
42
|
|
|
->where('model_type', 'App\Models\BackpackUser') |
43
|
|
|
->where('model_id', $entry->model_id) |
44
|
|
|
->delete(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// for the rest of them, we can just replace the BackpackUser model with User |
48
|
|
|
DB::table($table_name) |
49
|
|
|
->where('model_type', "App\Models\BackpackUser") |
50
|
|
|
->update([ |
51
|
|
|
'model_type' => "App\User", |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|